1

I'm trying past few days to solve input number form validation in javascript. The logic user doesn't allow to enter repeated same number like "00000000000", "11111111111". If they enter numbers on text field i have to show error message,

sample code,

var mobNumber = $('#phNo').val();

if(mobNumber.match("00000000") || mobNumber.match("1111111")) {
  alert('Please enter valid phone number');
}
Sathya
  • 1,704
  • 3
  • 36
  • 58

4 Answers4

2

You could use following regex ^(\d)\1+$ :

  • ^ asserts position at start of the string
  • (...) 1st capturing group
  • \d matches a digit (equal to [0-9])
  • \1 matches the same text as most recently matched by the 1st capturing group
  • + Quantifier, matches between one and unlimited times, as many times as possible, giving back as needed
  • $ asserts position at the end of the string, or before the line terminator right at the end of the string (if any).

See following example:

function test(par){
  if(par.match(/^(\d)\1+$/g)){
    console.log(par + " is not valid");
  }else{
    console.log(par + " is valid");
  }
}

test("11111111");
test("11131111");
test("111a1111");
test("010101010");
test("9999");

I hope it helps you. Bye

Alessandro
  • 4,382
  • 8
  • 36
  • 70
0

You can simply write code like

$(document).on('input', '#phNo', function() {
    var mobNumber = $(this).val();
    var res = mobNumber/(mobNumber/10);
    if(res == 111111111) {
      alert('Please enter valid phone number');
    }
});

this is applicable for all numbers and you have to check the max and min length of the input ..

Mujthaba Ibrahim
  • 207
  • 3
  • 16
0

try this :

var checkPhone = function() {
  phone_number = $('#phone').val();
  res = (/^(.)\1+$/.test(phone_number) ? '1' : '0');
  if(res == '1'){
    return 'bad phone number';
  } else {
    return 'good phone number';
  }
}

Test it here : JSFIDDLE

clinton3141
  • 4,751
  • 3
  • 33
  • 46
kanzari
  • 87
  • 4
0

You can try like this,

var phone = "11111111";
            var phonecount = phone.length;
            var countLength = 0;
            for (var i in phone)
            {
                if(phone.substring(0,1)==phone[i])
                {
                    countLength = countLength + 1;
                }
            }
            if (countLength == phonecount)
            alert("Please enter valid phone number");
Amol B Lande
  • 242
  • 1
  • 2
  • 13