Jana
Jana

Reputation: 53

How to validate mobile number in Regular Expression

I am trying to validate mobile using regular expression

so for i have tried https://regex101.com/#javascript

My Expresion is ((\d[^0-5]|[6-9])\d{9})|^(\d)\1*$

i need validate moblie number like below

1.it should not start with 0-5

e.g 0600432102

2.not all the same or in sequence

e.g 1111111111 or 0123456789 or 9876543210

3.lenght is 10 digit

where i made error.

help me....

thanks ....

Upvotes: 1

Views: 772

Answers (4)

james jelo4kul
james jelo4kul

Reputation: 829

This regex works for repititions and numbers not starting with 0-5.

(?!([0-9])\1{9})(\b[6-9]\d{9})

See demo here: https://regex101.com/r/uJ0vD4/9

It doesn't detect ascending and descending numbers. You can achieve that using a loop in your program

Upvotes: 0

Eirik Birkeland
Eirik Birkeland

Reputation: 608

This covers all criteria and tests a few numbers. It does however not specify the reason for a number being invalid - I leave that to you.

var numArr = ["1111111111", "0123456789", "9876543210", "8682375827", "83255"];

for (var i = 0; i < numArr.length; i++) {
    console.log(numArr[i] + " is " + validate(numArr[i]));
}

function validate(num) {
    if ((num.match(/^[6-9]\d{9}$/) && (!num.match(/^([0-9])\1{9}$/))) && (!isIncr(num) && (!isDecr(num)))) {
        return ( "Valid") ;
    } else {
        return ( "Not valid") ;
    }
}

function isIncr(num) {
    for (var i = 1; i < num.length; i++) {
        if (num[i] == parseInt(num[i - 1]) + 1) {
            continue;
        } else {
            return false;
        }
    }
    return true;
}

function isDecr(num) {
    for (var i = 1; i < num.length; i++) {
        if (num[i] == parseInt(num[i - 1]) - 1) {
            continue;
        } else {
            return false;
        }
    }
    return true;
}

Upvotes: 1

Adam
Adam

Reputation: 18855

you can use the following regex:

 /^(?!9876543210)(?!([\d])\1{9})[6-9][\d]{9}$/mg

Explanation

(?!9876543210) Check that the string is different (it's the only sequence possible)

(?!([\d])\1{9}) Check that this is not the same number repeated

[6-9][\d]{9} Check that the number start with 6 to 9, followed by 9 numbers

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80657

(([6-9])(?!\2{9})\d{9})

will:

  1. Check if the number starts with 6-9. It stores this in match group 2.
  2. Check that the first digit is not followed by exactly 9 (you can set the limits here) repetitions of the same digit.
  3. Continues to find if there are 9 more digits after the first digit.

will not:

  1. Check whether the numbers are ascending/descending order.
  2. Check for patterns such as 6566666666

Upvotes: 1

Related Questions