Reputation: 17289
i have written this regular expression to validating user mobile number but it doesn't work correctly.
String mobileNumber = '09328076432';
Pattern pattern = r'^(?:[+0]9)?[0-9]{10}$';
RegExp regex = RegExp(pattern);
print(regex.hasMatch(mobileNumber));
validating all of character should be integer, length should be 11 character and starting with 09
Upvotes: 2
Views: 8336
Reputation: 483
Validation using Regex:
String validateMobile(String value) {
String pattern = r'(^(?:[+0]9)?[0-9]{10,12}$)';
RegExp regExp = new RegExp(pattern);
if (value.length == 0) {
return 'Please enter mobile number';
}
else if (!regExp.hasMatch(value)) {
return 'Please enter valid mobile number';
}
return null;
}
Upvotes: 2
Reputation: 37755
Your regex is trying to match 09
followed by 10
digits, whereas you're willing to match a string which is 09
followed by 9
digits
So you can change your regex to this
^\+?09[0-9]{9}$
^
- Start of string\+?
- Match +
( optional )09
- Match literally 09
[0-9]{9}
- Match any digits between 0 to 9, 9 times$
-End of stringUpvotes: 5