jQuery中的验证号

时间:2013-07-27 21:57:33

标签: jquery

我正在尝试在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应该是数字,如果不显示meessage

2 个答案:

答案 0 :(得分:0)

在规则(see docs for list of validator rules

中验证为整数使用digits: true
rules: {
    ...
    pId: {
        minlength: 2,
        required: true,
        digits: true
    },
    dId: {
        minlength: 2,
        required: true,
        digits: true
    }
},

答案 1 :(得分:-1)

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

if ((/[^0-9\-\(\)\.\_\+\ ]/.test(number))) {
    $("#ContentPlaceHolder1_lblError").empty();              
    $("#ContentPlaceHolder1_lblError").append(var_IsNumericContactnowithCh);
    $("html, body").animate({
        scrollTop: 0
    }, 600);
    return false;
}