仅数字字段中允许使用符号吗?
我正在使用低音阻抗形式验证。我有几个仅需要数字的字段,但不允许使用 $、(、)、-、* 等符号,并且我需要这些字段来表示价格和电话号码等字段。有人知道解决这个问题的方法或插件或其他东西吗?
更新
基本上,我需要一个函数来表示“不允许使用文本”以及允许的符号和数字。
另一个更新
发现一个不允许单独使用文本的功能。但如果有符号,它也允许文本。有什么办法限制任何文本吗?
jQuery.validator.addMethod(
"symbols",
function(value,element){
var hNum=/[^a-z\s-]/;
var inp=jQuery.trim(element.value).toLowerCase();
if(hNum.test(inp)) { return true;
}
else return false;
},
"Number and symbols are only allowed."
);
Im using the bassistance form validation. I have a couple of fields that require digits only, but that excludes symbols like $,(,),-,*, etc from being allowed and i need these for fields like prices and phone numbers. Anyone know a method or plugin or something to work around this?
UPDATE
Basically, i need a function that says "no text allowed" and allowed symbols and numbers.
Another update
Found a function that doesnt allow text alone. But if there are symbols, it allows text as well. Any way to restrict any text?
jQuery.validator.addMethod(
"symbols",
function(value,element){
var hNum=/[^a-z\s-]/;
var inp=jQuery.trim(element.value).toLowerCase();
if(hNum.test(inp)) { return true;
}
else return false;
},
"Number and symbols are only allowed."
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我为它写了一个小函数...
当然,您可以根据需要调整它。 :)
编辑:
以下是其他有用的正则表达式:
/^\s*(\+|-)?\d+\s*$/
?/^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/
?I wrote a little function for it...
You can adapt it as you wish, of course. :)
Edit:
Here are other useful reg-exes:
/^\s*(\+|-)?\d+\s*$/
?/^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/
?根据他们的文档,已经有验证方法可以执行验证: 十进制数字,数字 和 电话号码
**编辑:在这种情况下,听起来您想创建自定义验证。有一种名为 addMethod 的方法,它通过调用添加自定义验证 - back 函数就像 daGrevis 创建的函数一样。
示例:
According to their documentation, there's already validation methods that can perform validation for: decimal numbers, digits and phone numbers
**EDIT: In that case it sounds like you want to create a custom validation. There is a method called addMethod which adds a custom validation with a call-back function like the one daGrevis created.
Example:
好吧,您可以尝试完全不同的验证方法。
我个人最喜欢的是使用 HTML5 的验证并使所有浏览器都支持/扩展其功能。
http://ericleads.com/h5validate/
Well, you might try a different approach to validation entirely.
My personal favorite is to use HTML5's validation and make all browsers jive/extend its functionality.
http://ericleads.com/h5validate/