Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 theString 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 theString 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 theStringValidation extension:

extensionStringValidationonString{boolgetisValidEmail{finalregex=RegExp(r'^[^@]+@[^@]+\.[^@]+');returnregex.hasMatch(this);}boolgetisNumeric{finalregex=RegExp(r'^-?[0-9]+$');returnregex.hasMatch(this);}boolgetisPhoneNumber{finalregex=RegExp(r'^\+?[0-9]{10,13}$');returnregex.hasMatch(this);}}
Enter fullscreen modeExit 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:

voidmain(){varemail="example@test.com";varphone="+1234567890";varnumber="12345";print(email.isValidEmail);// Output: trueprint(phone.isPhoneNumber);// Output: trueprint(number.isNumeric);// Output: true}
Enter fullscreen modeExit fullscreen mode

Explanation

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

Conclusion

By using Dart's extension methods, you can add reusable validation logic to theString 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromfrancesco agati

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp