Reputation: 53
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
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
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
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
Reputation: 80657
(([6-9])(?!\2{9})\d{9})
will:
6-9
. It stores this in match group 2
.9
(you can set the limits here) repetitions of the same digit.will not:
6566666666
Upvotes: 1