Zaveed Abbasi Zaveed Abbasi - 7 months ago 8
Javascript Question

Regular Expression to Check numeric field doesn't have /or have all same digits

I have to validate a field. The field should be numeric, should not contain all the same digits (like 555555, 111111) . I need a regular expression to check this. I need to know how can I check if it contains all same digits.

tried few solutions like following but didn't worked

these three didn't work

var reg_exp = new RegExp('/^([0-9a-z])\1+$/gm');

var reg_exp = new RegExp('/^(\d)\1*$/');

var reg_exp = new RegExp('([0-9])\\1{5}');


This one working partly bcoz here it fixes to specific number of digits e.g. {6} in the expample to a specific number but i need a solution so that i can apply this to any number of digits

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = "111111112";
var reg_exp = new RegExp('([0-9]){6}');
alert(reg_exp.test(str));
}
</script>
</body>
</html>


based on http://stackoverflow.com/users/3832970/wiktor-stribi%C5%BCew tried this and it worked for string which have length more than 2

function myFunction() {
var str = "222";
var reg_exp = /^(\d)\1+$/; // new RegExp('([0-9]){6}');
alert(reg_exp.test(str));
}


returns
true
and

function myFunction() {
var str = "221";
var reg_exp = /^(\d)\1+$/; // new RegExp('([0-9]){6}');
alert(reg_exp.test(str));
}


returns
false


but the issue is that for

function myFunction() {
var str = "22";
var reg_exp = /^(\d)\1+$/; // new RegExp('([0-9]){6}');
alert(reg_exp.test(str));
}


or

function myFunction() {
var str = "11";
var reg_exp = /^(\d)\1+$/; // new RegExp('([0-9]){6}');
alert(reg_exp.test(str));
}


it returns
false


I need a regex which can work for one digits numbers as well. and
var str = "1";
or
var str = "22";
should return
true
. Basically the numbers should have atleast two
unique
digits

Answer

A regex that will match an all-numeric string that has no 2 unique digits is

/^(\d)(?:\1+)?$/

See the regex demo.

Explanation:

  • ^ - start of string
  • (\d) - Group 1 that captures a single digit (the first one in the string)
  • (?:\1+)? - an optional (as ? matches one or zero occurrences) non-capturing group that matches the same digit that was captured with Group 1
  • $ - end of string.

In JS:

document.body.innerHTML = "22: " + /^(\d)(?:\1+)?$/.test("22") + " as there are no unique digits, 2-digit string";
document.body.innerHTML += "<br/>12: " + /^(\d)(?:\1+)?$/.test("12") + " as there are 2 unique digits";
document.body.innerHTML += "<br/>1: " + /^(\d)(?:\1+)?$/.test("1") + " as there is just 1 digit, no 2 unique digits";
document.body.innerHTML += "<br/>-------- OR, with negation ----------";//or
document.body.innerHTML += "<br/>22: " + !/^(\d)(?:\1+)?$/.test("22") + " as there are no unique digits, 2-digit string";
document.body.innerHTML += "<br/>12: " + !/^(\d)(?:\1+)?$/.test("12") + " as there are 2 unique digits";
document.body.innerHTML += "<br/>1: " + !/^(\d)(?:\1+)?$/.test("1") + " as there is just 1 digit, no 2 unique digits";