In my Flutter mobile app, I am trying to validate a phone number using regex
. Below are the conditions.
In simple terms, here is are the only "valid" phone numbers
0776233475
, +94776233475
, 094776233475
Below is what I tried, but it do not work.
String _phoneNumberValidator(String value) { Pattern pattern = r'/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/'; RegExp regex = new RegExp(pattern); if (!regex.hasMatch(value)) return 'Enter Valid Phone Number'; else return null; }
How can I solve this?
/^([+]\d{2})? \d{10}$/ This is how this regex for mobile number is working. + sign is used for world wide matching of number.
Visit www.textmagic.com or download the TextMagic mobile app on google play store. Enter your phone number and country and click on Validate Number. This app will show you the status of the number if it is active or not.
You could make the first part optional matching either a +
or 0 followed by a 9. Then match 10 digits:
^(?:[+0]9)?[0-9]{10}$
^
Start of string(?:[+0]9)?
Optionally match a +
or 0
followed by 9[0-9]{10}
Match 10 digits$
End of stringRegex demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With