checking if number entered is a digit in jquery

前端 未结 10 1683
遥遥无期
遥遥无期 2020-12-01 01:19

I have a simple textbox in which users enter number.
Does jQuery have a isDigit function that will allow me to show an alert box if users ente

相关标签:
10条回答
  • 2020-12-01 01:54

    Value validation wouldn't be a responsibility of jQuery. You can use pure JavaScript for this. Two ways that come to my mind are:

    /^\d+$/.match(value)
    Number(value) == value
    
    0 讨论(0)
  • 2020-12-01 02:00
    $(document).ready(function () {
    
    
    
        $("#cust_zip").keypress(function (e) {
            //var z = document.createUserForm.cust_mobile1.value;
            //alert(z);
            if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
    
                $("#errmsgzip").html("Digits Only.").show().fadeOut(3000);
                return false;
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-01 02:01

    Forget regular expressions. JavaScript has a builtin function for this: isNaN():

    isNaN(123)           // false
    isNaN(-1.23)         // false
    isNaN(5-2)           // false
    isNaN(0)             // false
    isNaN("100")         // false
    isNaN("Hello")       // true
    isNaN("2005/12/12")  // true
    

    Just call it like so:

    if (isNaN( $("#whatever").val() )) {
        // It isn't a number
    } else {
        // It is a number
    }
    
    0 讨论(0)
  • 2020-12-01 02:02
    String.prototype.isNumeric = function() {
        var s = this.replace(',', '.').replace(/\s+/g, '');
    return s == 0 || (s/s);
    }
    

    usage

    '9.1'.isNumeric() -> 1
    '0xabc'.isNumeric() -> 1
    '10,1'.isNumeric() -> 1
    'str'.isNumeric() -> NaN
    
    0 讨论(0)
提交回复
热议问题