TEA COFFEE
TEA COFFEE

Reputation: 31

I want to validate phone number using regExp

The phone number should start with 0 followed by 6 or 7 and should contain 10 digits only

Here are some sample phone numbers

0754758644 ,0621165600

Here is what I tried

String pattern = r'(^(?:[0]9)?[0-9]{10,12}$)';

Upvotes: 1

Views: 73

Answers (4)

The fourth bird
The fourth bird

Reputation: 163362

You can match a zero, then either 6 or 7 using character class followed by 8 digits.

For a match only, you can omit the capture group.

String pattern = r'^0[67]\d{8}$' 

Upvotes: 0

farman zia
farman zia

Reputation: 11

String pattern = r'^(?:[+0][1-9])?[0-9]{10,12}$';
RegExp regExp = new RegExp(patttern);

regExp.hasMatch(value)

or you can apply keyboardtype property to textfield or textformfield if you are not familiar with regExp

Upvotes: 1

Martin del Necesario
Martin del Necesario

Reputation: 265

this regex should work for you:

String pattern = r'(^0(6|7)\d{8}$)'

I limited the digits to 8, because the 0 and the 6/7 already take up two digits of the length. The {8} only limits the direct predecessor (the digits matcher \d).

find out more about this regex here: regex101

Upvotes: 1

ABDUL Aleem
ABDUL Aleem

Reputation: 21

Use this regex pattern -

String pattern = r'([0][6,7]\d{8})

Upvotes: 0

Related Questions