Search code examples
javascriptintegerisinteger

Check string for integer


I want to validate a input field. The user should type in a phone number with minimum length of 10 digits.

So I need to check for illegal chars. It would be nice just to check wheather the input is an integer or not.

I came up with this but it does not work (n would be the string).

function isInt(n){
    return typeof n== 'number' && n%1==0;
}

Any ideas?


Solution

  • You can do a test like this:

    input.length >= 10 && /^[0-9]+$/.test(input)
    

    That will fail if there are non-digits in the string or the string is less than 10 chars long