14

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>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
  • just a note- your question title contains Bangladesh as country name whereas India is in the body. Is this a mistake or pun intended? – maksbd19 Aug 11 '16 at 10:02

10 Answers10

28

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

Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
18

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}$/)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Have you tried my suggestion? Does it work as expected on your side? If not, please let me know. Best regards. – Wiktor Stribiżew Jun 05 '15 at 11:19
  • thnx finally it works for me, for bKash mobile no. validation it was exactly need – X-Coder Aug 12 '15 at 06:51
  • Glad it did, please consider accepting the answer then. – Wiktor Stribiżew Aug 12 '15 at 07:00
  • Sorry there is a problem yet. 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... plz fix it. Thnx @stribizhev here is my code:

    Not valid mobile no.!

    – X-Coder Sep 19 '15 at 04:21
  • 2
    Please check my update. If you need to also support `+` before `01`: [`/^\+(?:88|01)?(?:\d{11}|\d{13})$/`](https://regex101.com/r/cD5pE5/3). If the plus is optional: [`/^(?:\+?(?:88|01)?)(?:\d{11}|\d{13})$/`](https://regex101.com/r/cD5pE5/2). – Wiktor Stribiżew Sep 20 '15 at 11:51
7

A better answer can be found here: Validate Mobile number using regular expression

There is two good answer in this post. Summary of them

Solution-1: Using regular expression

Should be pretty simple:

^(?:\+?88|0088)?01[15-9]\d{8}$
  • ^ - From start of the string
  • (?:\+?88|0088)? - optional 88, which may begin in + or optional 0088
  • 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

Solution-2: Using Library

Download this free library libphonenumber from Google. It's available for Java, C++ and Javascript, but there're also fork for PHP and, i believe, other languages.

+880 tells me that it's country code for Bangladesh. Let's try to validate example numbers with following code in Java:

String bdNumberStr = "8801711419556";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
    //BD is default country code for Bangladesh (used for number without 880 at the begginning)
    PhoneNumber bdNumberProto = phoneUtil.parse(bdNumberStr, "BD");
} catch (NumberParseException e) {
    System.err.println("NumberParseException was thrown: " + e.toString());
}
boolean isValid = phoneUtil.isValidNumber(bdNumberProto); // returns true

That code will handle also numbers with spaces in it (for example "880 17 11 41 95 56"), or even with 00880 at the beggininng (+ is sometimes replaced with 00).

Community
  • 1
  • 1
Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48
1

Use this regular expression "^(?:+88|88)?(01[3-9]\d{8})$" this cover all Bangladeshi's mobile operators, like:

  • 013xxxxxxxx
  • 014xxxxxxxx
  • 014xxxxxxxx
  • 016xxxxxxxx
  • 017xxxxxxxx
  • 018xxxxxxxx
  • 019xxxxxxxx

Example:

String mobileNumber ="01715254815";

if(mobileNumber.matches("^(?:\\+88|88)?(01[3-9]\\d{8})$")){
    System.out.println("Yes valid");
}
Subarata Talukder
  • 5,407
  • 2
  • 34
  • 50
1

My JavaScript Solution for Bangladeshi All mobile number validation with Extra Bangla to English convert. This solution also with for Bangla number

Few Example and example output

valid_mobile('+8801736458080');
/* Output "01736458080" */

valid_mobile('8801736458080');
/* Output "01736458080" */

valid_mobile('01736458080');
/* Output "01736458080" */


valid_mobile('+৮৮০১৭৩৬৪৫৮০৮০');
/* Output "01736458080" */



/*
    * Validate Bangladeshi mobile number
    * @author: Lincoln Mahmud
    * @company: Purple Patch
*/

function valid_mobile ( value ) {
    /*When value not number then try to convert bangla to english number*/
    if (isNaN(value)) {
        value = translteBanglaToEngNum(value);
    }
    valid_number = value.match("(?:\\+88|88)?(01[3-9]\\d{8})"); /*Regular expression to validate number*/
    /*When valid return without +88/88 number if exist*/
    if(valid_number){
        return valid_number[1]; /*valid number method return 3 with actual input*/
    } else {
        return false; /*return false when not valid*/
    }
}

/*
    * Bangla to English number conversion method
    * @author: Lincoln Mahmud
    * @company: Purple Patch
*/

function translteBanglaToEngNum( num_str ){
    var bengali = {"০":0, "১":1, "২":2, "৩":3, "৪":4, "৫":5, "৬":6, "৭":7, "৮":8, "৯":9};
    var changed_nun='';
    num_str.toString().split('').forEach(l => {
        if(isNaN(l)){changed_nun += bengali[l];}else{changed_nun +=l;}
    });
    return changed_nun;
}
Josef
  • 2,869
  • 2
  • 22
  • 23
lincolndu
  • 93
  • 1
  • 8
  • PHP solution for Bangladeshi Mobile validation (Exact solution part) `function valid_mobile ( $value ) { preg_match("/^(?:\\+88|88)?(01[3-9]\\d{8})/",$value, $result); if($result){ return $result[1]; /*return exact validate number*/ } else { return false; /*return false when not valid*/ } } var_dump(valid_mobile('+8801373645808325324'));` – lincolndu Jul 05 '21 at 15:57
1

This regex will valid following bellow condition

  • +880 1760 123128
  • +(+880)-1760-123128
  • +8801996409999
  • +8801760-123128
[+,' '][\d]{7}[-,$,' ',][\d]{0,6}|[+,' '][\d]{3}[,-,$,' ',][\d]{4}[-,$,' ',][\d]{0,6}|[(][(+,' '][\d]{3}[)][-,$,' '][\d]{4}\b[-,$,' ',][\d]{0,6}|[[+,' '][\d]{3}[-,$,' ',][\d]{4}[-,$,' ',][\d]{0,6}|[(][(+,' '][\d]{3}[)][-,$,' '][\d]{4}\b[-,$,' ',][\d]{0,6}|[+' '][\d]{0,13}|[+,' '][\d]{0,7}[-,$,' ',][\d]{0,6}
nerd_lab
  • 11
  • 3
1

for Bangladeshi phone number you can use this /^(01|008801|8801|\+8801)[0-9]{9}$/

Roky
  • 11
  • 1
0

basically, Bangladesh country phone code is 880, then 10 digit. It may start with '+' or '00' for the international call.

Country code does not use for local call but must be 0 at first.

so the regex will be like this below:

^(((\+|00)?880)|0)(\d){10}$
Rousonur Jaman
  • 1,183
  • 14
  • 19
0
/^01[3-9]\d{8}$/g // Check BD phone number without +88
/^(?:\+?88)?01[3-9]\d{8}$/g // Check BD phone number with +88
Ovi
  • 179
  • 1
  • 4
-3

Use Regular Expressions and match with mobile number string.

string mobileNumber = "+8801000000000";
if (new Regex(@"^(?:\+?88|0088)?01[15-9]\d{8}$").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);
}
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
  • 2
    This will not make sure that `number` has `01` before it.It will only match `0` or `1` – vks Jun 05 '15 at 05:31