0

我正在尝试在 jQuery 中进行验证,以便只输入数字而不是文本。但我不知道该怎么做。

\$('#id_form').validate(
 {
  rules: {
   name: {
      minlength: 2,
      required: true
    },
   surname: {
      minlength: 2,
      required: true
    },
    gender: {
      required: true
    },
   pId: {
      minlength: 2,
      required: true
    },
    dId: {
      minlength: 2,
      required: true
    }
      },
 highlight: function(element) {
    \$(element).closest('.control-group').removeClass('success').addClass('error');
  },
  success: function(element) {
    element
    .text('').addClass('valid')
    .closest('.control-group').removeClass('error').addClass('success');
  }
 });

pId snd dId 应该是数字,如果不显示消息

4

2 回答 2

0

To validate as integer use digits: true in rule (see docs for list of validator rules)

rules: {
    ...
    pId: {
        minlength: 2,
        required: true,
        digits: true
    },
    dId: {
        minlength: 2,
        required: true,
        digits: true
    }
},
于 2013-07-27T22:15:37.970 回答
-1

以下是仅接受号码的代码:

if ((/[^0-9\-\(\)\.\_\+\ ]/.test(number))) {
    $("#ContentPlaceHolder1_lblError").empty();              
    $("#ContentPlaceHolder1_lblError").append(var_IsNumericContactnowithCh);
    $("html, body").animate({
        scrollTop: 0
    }, 600);
    return false;
}
于 2017-04-21T06:07:04.790 回答