Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If only numeric values were entered in input. (jQuery)

I would like to check if users enter correct phone number in, with help of jQuery, so far I got to this stage:

var phone = $("input#phone").val();
if (phone !== "") {
    //Check if phone is numeric
    $("label#phone_error").show(); //Show error
    $("input#phone").focus(); //Focus on field
    return false;
}

Basically it checks if phone number was entered and if it was, I would like to check if it is a numeric value and if it is not display the error messages. Could anyone help with checking if it is numeric?

like image 838
Ilja Avatar asked Sep 02 '25 14:09

Ilja


1 Answers

Try this ... it will make sure that the string "phone" only contains digits and will at least contain one digit

if(phone.match(/^\d+$/)) {
    // your code here
}
like image 93
devnull69 Avatar answered Sep 05 '25 05:09

devnull69