jQuery验证器仅允许点(。)和数字 [英] jQuery validator allow dot (.) and numbers only

查看:72
本文介绍了jQuery验证器仅允许点(。)和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个输入,用户可以投标金额。



目前它允许用户输入数字而不是点分隔符。



这是我的jQuery代码:



Hi,

I have an input where users can bid an amount of money.

currently it allows the user to enter numbers but not a dot seperator.

This is my jQuery code:

$().ready(function() {
	//validate register form on keyup and submit
	$("#postItem").validate({
		rules:{
			itemName:{
				required: true,
				minlength: 5
			},
			itemDesc:{
				required: true,
				minlength: 30
			},
			startBid: {
				required: true,
				digits: true
			},
		},
		messages: {
			itemName: {
				required: "Please provide a name for your item",
				minlength: "The item name must be longer than 4 characters<br>"
			},
			itemDesc:{
				required: "You need to provide a description of your item",
				minlength: "The description needs to be more than 30 characters<br>"
			},
			startBid:{
				required: "A starting bid amount is required",
				digits: "Only numerical values allowed"
			},
		},
	});

});



我可以使用除数字以外的规则吗?


Is there a rule other than digits that I can use?

推荐答案

()。ready( function (){
// 验证密钥上的注册表并提交
().ready(function() { //validate register form on keyup and submit


#postItem)。validate({
rules:{
itemName:{
required: true
minlength: 5
},
itemDesc:{
required: true
minlength: 30
},
startBid:{
required: true
位数: true
},
},
消息:{
itemName:{
required: 请提供项目名称
minlength: 项目名称必须超过4个字符< br>
},
itemDesc:{
required: 您需要提供项目的描述
minlength: 描述需要超过30个字符< br>
},
startBid:{
required: 起始出价金额为
digits: 只允许数值
},
},
});

});
("#postItem").validate({ rules:{ itemName:{ required: true, minlength: 5 }, itemDesc:{ required: true, minlength: 30 }, startBid: { required: true, digits: true }, }, messages: { itemName: { required: "Please provide a name for your item", minlength: "The item name must be longer than 4 characters<br>" }, itemDesc:{ required: "You need to provide a description of your item", minlength: "The description needs to be more than 30 characters<br>" }, startBid:{ required: "A starting bid amount is required", digits: "Only numerical values allowed" }, }, }); });



我可以使用除数字以外的规则吗?


Is there a rule other than digits that I can use?


请看到我对这个问题的评论。您可以简单地过滤掉不需要的字符,并允许您想要的任何内容,而不是验证。这个简短的教程解释了所有内容: http://www.javascripter.net/faq/keyboardinputfiltering.htm 。 />






您可以处理任何字符和/或按键。

有一些额外的字符或键来检查?去做就对了。如果您不想使用正则表达式,请在单独的表达式中计算它并将它放在if下的布尔表达式中。



Dot is :

Please see my comments to the question. Instead of validation, you can simply filter out the unwanted characters and allow whatever you want. This short tutorial explains everything: http://www.javascripter.net/faq/keyboardinputfiltering.htm.



You can handle any characters and/or key presses.
Having some additional characters or keys to check up? Just do it. If you don't want to mess with Regular Expression, calculate it in a separate expression and put it together in a Boolean expression under your "if".

Dot is:
For keypress: event.charCode == 46; event.which == 46;
For keyup: event.keyCode == 190; event.which == 190;





您还应该确保允许退格:



You also should make sure you allow backspace:

For keypress: event.keyCode == 8; event.which == 8;
For keyup: event.keyCode == 8; event.which == 8;



不是 event.keyCode event.charCode 是不同的东西。并非所有的键都有与之相关的字符。



如何知道所有这些事实?使用此测试页: http://www.asquare.net/javascript/tests/KeyCode.html



-SA


Not that event.keyCode and event.charCode are different things. Not all keys have characters associated with them.

How to know all these facts? Use this test page: http://www.asquare.net/javascript/tests/KeyCode.html.

—SA


这篇关于jQuery验证器仅允许点(。)和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆