Javascript jquery 仅验证数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3456382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:42:22  来源:igfitidea点击:

jquery validation only digits

javascriptjqueryjquery-pluginsjquery-validate

提问by maas

I am having the jquery.validate.js plugin and it is working fine for me.

我有 jquery.validate.js 插件,它对我来说工作正常。

My question is :

我的问题是:

I am having a textbox and this textbox is mandatory and should only accept digits.

我有一个文本框,这个文本框是强制性的,应该只接受数字。

In the js, I can see that there is validation for the digits and the problem is that I do not know how to use it.

在js中,我可以看到对数字进行了验证,问题是我不知道如何使用它。

I only knew how to make the field required by putting in the textbox field <class="required">

我只知道如何通过放入文本框字段来制作所需的字段 <class="required">

So, how can I add one more validation criteria to accept only digits.

那么,我怎样才能再添加一个验证标准来只接受数字。

Thanx

谢谢

回答by Haim Evgi

to check it add

检查它添加

$("#myform").validate({
  rules: {
    amount: {
      required: true,
      digits: true
    }
  }
});

http://docs.jquery.com/Plugins/Validation/Methods/digits

http://docs.jquery.com/Plugins/Validation/Methods/digits

回答by Vignesh V

$("#mobile_number").validate({
            rules: {

                mobile_number: {required: true,number:true}
            },
            messages: {
                mobile_number: {required: "Please Enter Your Mobile Number",number:"Please enter numbers Only"}
            }

 });