Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 997
UpCat Avatar asked Dec 03 '22 09:12

UpCat


1 Answers

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

like image 173
Martin Jespersen Avatar answered Dec 21 '22 08:12

Martin Jespersen