在jquery中,将文本框验证为数字

In jquery validation for textbox as numeric

本文关键字:验证 数字 文本 jquery      更新时间:2023-09-26

只对数值进行用户ID验证!这是我的代码,请告诉我我的代码哪里出错了

function login() {
    UserID = $('#txtLoginScreen_UserId').val();
    password = $('#txtLoginScreen_MPIN').val();

    if (UserID.length == 0) {
        alert("Please enter User ID");
    } else if (password.length == 0) {
        alert("Please enter MPIN");
    }
    /* Here am unable to make USer ID validation for restricting only numeric values upto 12  */
    /* else if (isNaN(parseInt($("#txtLoginScreen_UserId").val()))) {
     alert('It must be numbers');
     return false;
     }*/
    /* Validate for UserID  */
    /*  $("#txtLoginScreen_UserId").keypress(function(e) {
     alert("validate");
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
     //display error message
     $("#errmsg").html("Digits Only").show().fadeOut("slow");
     return false;
     }
     });
     */
    else {
}
if()$.isNumeric(UserID){
}  else{
alert("not numaric");
}

试试这个,

if (parseInt(UserID) > 0) {
   alert("Please enter alphanumeric characters only");
}