Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Bangladeshi phone number with optional +88 or 01 preceeding 11 digits

I am using the following regular expression to validate an Indian phone number.

I want optional +88 or 01 before 11 digits of phone.

Here is what I am using:

string mobileNumber = "+8801000000000";
if (new Regex(@"^([01]|\+88)?\d{11}").IsMatch(mobileNumber)){
    MessageBox.Show("Mobile number is valide", "All information is required", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else{
     MessageBox.Show("Mobile number is not valide", "All information is required", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

How can I do it?

UPDATE

If I write mobile no. preceding 01, for more than 11 digit it notify validation mgs(Not valid mobile no.!). Well I used, it fails when 13 digit. It alters validation mgs wrongly.

Here is my code:

<input type="text" placeholder="Enter bKash wallet number" 
  class="form-control" ng-model="BkashWalletNo" ng-disabled="AutoConfirmed" 
    name="BkashWalletNo" ng-pattern="/^(?:\+88|01)?\d{11}\r?$/" />
<p class="help-block" ng-show="form.BkashWalletNo.$error.pattern">Not valid mobile no.!</p>
like image 534
Md Imran Choudhury Avatar asked Jun 05 '15 05:06

Md Imran Choudhury


2 Answers

You may use any one of given solution to validate Bangladeshi mobile number.

regular expression 1:

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

regular expression 2

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

regular expression 3

 (^([+]{1}[8]{2}|0088)?(01){1}[3-9]{1}\d{8})$

Allowed mobile number sample

+8801812598624

008801812598624

01812598624

01712598624

01672598624

01919598624

01419598624

01319598624

etc

like image 136
Majbah Habib Avatar answered Nov 14 '22 02:11

Majbah Habib


I see you have tried but your regex is not accurate.

  1. You do not use an end of string $ anchor (thus, even "abc" at the end will not prevent the IsMatch from returning true)
  2. You are using 01 inside square brackets thus creating a character class, meaning either 0 or 1.
  3. No need in a capturing group here, a non-capturing is best for optional subpatterns.
  4. As has been pointed out in the follow up answers, the regex you are using is not actually meeting all the requirements for the Bangladeshi phone number, see Kobi's ^(?:\+?88|0088)?01[15-9]\d{8}$ answer.

In order to create a regex that will validate a string that has "optional +88, 0088 or 01 preceeding 11 digits", you need something like:

@"^(?:(?:\+|00)88|01)?\d{11}$"

See RegexStorm demo

UPDATE

If you want to validate Bangladeshi phone numbers with this regex, nothing changes in the pattern (only \r? is totally redundant), but if you plan to allow 13 or 11 digits after +88 or 01, you need to use an alternation:

ng-pattern="/^(?:(?:\+|00)88|01)?\d{11}$/"

See demo

In Angular:

Validators.pattern('(?:(?:\\+|00)88|01)?\\d{11}')
// or
Validators.pattern(/^(?:(?:\+|00)88|01)?\d{11}$/)
like image 13
Wiktor Stribiżew Avatar answered Nov 14 '22 01:11

Wiktor Stribiżew