DEV Community

francesco agati
francesco agati

Posted on

Extending String for Validation in Dart 3

In Dart 3, one of the powerful features you can leverage is extensions. Extensions allow you to add new functionality to existing libraries. In this article, we will demonstrate how to extend the String class to include common validation checks such as email validation, numeric check, and phone number validation.

Creating the String Extension

Let's start by creating an extension on the String class. We will define three validation methods:

  1. isValidEmail - Checks if the string is a valid email format.
  2. isNumeric - Checks if the string consists only of numeric characters.
  3. isPhoneNumber - Checks if the string is a valid phone number.

Here's the code for the StringValidation extension:

extension StringValidation on String {
  bool get isValidEmail {
    final regex = RegExp(r'^[^@]+@[^@]+\.[^@]+');
    return regex.hasMatch(this);
  }

  bool get isNumeric {
    final regex = RegExp(r'^-?[0-9]+$');
    return regex.hasMatch(this);
  }

  bool get isPhoneNumber {
    final regex = RegExp(r'^\+?[0-9]{10,13}$');
    return regex.hasMatch(this);
  }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • isValidEmail: This method uses a regular expression to check if the string follows a basic email pattern (e.g., example@test.com).
  • isNumeric: This method checks if the string contains only numeric characters, including an optional leading minus sign for negative numbers.
  • isPhoneNumber: This method validates if the string is a phone number. It allows an optional leading plus sign and checks for a length between 10 to 13 digits.

Usage

Using these extensions is straightforward. You simply call the new methods on any string instance. Below is an example demonstrating how to use these validation methods:

void main() {
  var email = "example@test.com";
  var phone = "+1234567890";
  var number = "12345";

  print(email.isValidEmail);    // Output: true
  print(phone.isPhoneNumber);   // Output: true
  print(number.isNumeric);      // Output: true
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • email.isValidEmail: Checks if the email string is a valid email. The output will be true if the string matches the email pattern.
  • phone.isPhoneNumber: Validates if the phone string is a valid phone number. The output will be true if the string matches the phone number pattern.
  • number.isNumeric: Checks if the number string consists of numeric characters. The output will be true if the string matches the numeric pattern.

Conclusion

By using Dart's extension methods, you can add reusable validation logic to the String class. This approach makes your code cleaner and more modular. You can extend this pattern to include more validations as needed, enhancing the robustness of your applications.

Try integrating these extensions into your Dart projects and see how they can simplify your validation logic. Happy coding!

Top comments (0)