Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Mobile number using regular expression

Tags:

regex

I need to validate mobile number. My need:

  1. The number may start with +8801 or 8801 or 01
  2. The next number can be 1 or 5 or 6 or 7 or 8 or 9
  3. Then there have exact 8 digit.

How can i write the regular expression using this conditions ?

the mobile numbers I tried

+8801811419556
01811419556
8801711419556
01611419556
8801511419556
like image 239
G. M. Nazmul Hossain Avatar asked Feb 26 '13 11:02

G. M. Nazmul Hossain


People also ask

How can I check mobile number in RegEx?

/^([+]\d{2})? \d{10}$/ This is how this regex for mobile number is working.

How do I validate a phone number in SQL?

Inputs numbers should be 10 digits return message will be 'Valid No' When the mobile number starts with 9,8,7,6 - retrun Message 'Valid No' When the mobile number starts with 0,1,2,3,4,5 - return Message 'Invalid No Starts from 0-5' When mobile number = 000000000 - return message 'Invalid No'

How do you validate a regular expression?

To validate a RegExp just run it against null (no need to know the data you want to test against upfront). If it returns explicit false ( === false ), it's broken. Otherwise it's valid though it need not match anything.


2 Answers

Should be pretty simple:

^(?:\+?88)?01[15-9]\d{8}$
  • ^ - From start of the string
  • (?:\+?88)? - optional 88, which may begin in +
  • 01 - mandatory 01
  • [15-9] - "1 or 5 or 6 or 7 or 8 or 9"
  • \d{8} - 8 digits
  • $ - end of the string

Working example: http://rubular.com/r/BvnSXDOYF8

Update 2020

As BTRC approved 2 new prefixes, 013 for Grameenphone and 014 for Banglalink, updated expression for now:

^(?:\+?88)?01[13-9]\d{8}$
like image 138
Kobi Avatar answered Oct 10 '22 00:10

Kobi


You may use either one of given regular expression to validate Bangladeshi mobile number.

Solution 1:

/(^(\+88|0088)?(01){1}[56789]{1}(\d){8})$/

Robi, Grameen Phone, Banglalink, Airtel and Teletalk operator mobile no are allowed.

Solution 2:

 /(^(\+8801|8801|01|008801))[1|5-9]{1}(\d){8}$/

Citycell, Robi, Grameen Phone, Banglalink, Airtel and Teletalk operator mobile no are allowed.

Allowed mobile number pattern

+8801812598624

008801812598624

01812598624

01712598624

01919598624

01672598624

01512598624

................

.................

like image 34
Majbah Habib Avatar answered Oct 10 '22 02:10

Majbah Habib