jQuery 验证插件 - 仅数字、最小值和最小值最大长度 - 空间问题
我对大多数情况使用 jQuery Validation (PLUGIN URL) 插件我的表单验证:
我有以下示例(实例) :
FORM:
<form class="form" id="feedback-form" method="post" action="#">
<label for="phone">Contact number:</label>
<input type="text" value="" id="phone" name="phone" class="required">
<br class="clr">
<div class="form-buttons">
<input id="submit" type="submit" value="Submit" class="button" >
</div>
<br class="clr">
<br /><br />
</form>
jQuery:
$("#feedback-form").validate({
rules: {
phone: {
number: true, // as space is not a number it will return an error
minlength: 6, // will count space
maxlength: 9
}
}
});
我有两个问题,都与空间使用有关。
- min & 如果
number = true
并且使用了空格,最大长度会将空格算作一个字符 - ,它将返回错误,因为空格不是数字
有任何解决方法吗?你们中有人遇到过同样的问题吗?请注意,我想继续允许输入空格(为了便于阅读)。
I'm using jQuery Validation (PLUGIN URL) plugin for most of my form validation:
I have following example (LIVE EXAMPLE):
FORM:
<form class="form" id="feedback-form" method="post" action="#">
<label for="phone">Contact number:</label>
<input type="text" value="" id="phone" name="phone" class="required">
<br class="clr">
<div class="form-buttons">
<input id="submit" type="submit" value="Submit" class="button" >
</div>
<br class="clr">
<br /><br />
</form>
jQuery:
$("#feedback-form").validate({
rules: {
phone: {
number: true, // as space is not a number it will return an error
minlength: 6, // will count space
maxlength: 9
}
}
});
I have two issues, both relating to space usage.
- min & max length will count space as an character
- if
number = true
and space is used, it will return error as space is not a number
Is there any workaround this? Have any of you bumped in on the same problem? Please note I want to keep allowing to type in space (for readability).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加 beforeValidation 回调来发送不带空格的值,请查看此处: Jquery验证回调之前的验证器,这似乎就是您正在寻找的
You can add a beforeValidation callback to send the value without spaces, take a look here: Jquery Validator before Validation callback, it seems to be what you're looking for
一个非常简单的解决方案是:
将电话输入的类型替换为以下内容:
并且您永远不会遇到空格和其他内容的问题,只需在我的代码中使用它即可!
享受
a very smiple solution is :
replace the type of the phone input as the following :
and you would never have issues with spaces and stuff, just used that in my code !
enjoy