Created
February 12, 2022 12:21
-
-
Save pauloantonelli/7ed83ffe717464df3ea15c3d32f56c0a to your computer and use it in GitHub Desktop.
Usefull validators for dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Validators { | |
static bool validateEmail(String value) { | |
if (value.isEmpty) return false; | |
final String pattern = | |
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'; | |
final regex = RegExp(pattern); | |
final result = regex.hasMatch(value); | |
return result; | |
} | |
static bool validateField(String value) { | |
if (value.isEmpty) return false; | |
return true; | |
} | |
static bool validatePhone(String value) { | |
if (value.isEmpty) return false; | |
// Valid only for Br phone number "(99) 9999-9999" or "(99) 99999-9999" | |
final String pattern = r'^\(([0-9]{2,3})\)([ ])([0-9]{4,5})([-])([0-9]{4})'; | |
final regex = RegExp(pattern); | |
final result = regex.hasMatch(value); | |
return result; | |
} | |
static bool validatePassword(String value) { | |
if (value.isEmpty) return false; | |
final result = (value.length < 6 || value.length > 20) ? false : true; | |
return result; | |
} | |
} | |
extension StringExtension on String { | |
bool isValidEmail() { | |
return Validators.validateEmail(this); | |
} | |
bool isValidField() { | |
return Validators.validateField(this); | |
} | |
bool isValidPhone() { | |
return Validators.validatePhone(this); | |
} | |
bool isValidPassword() { | |
return Validators.validatePassword(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment