jQuery 验证插件 - 仅数字,最小和最大长度 - 空间问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7318024/
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
jQuery validation plugin - numbers only, min & max length - space issue
提问by Iladarsda
I'm using jQuery Validation (PLUGIN URL) plugin for most of my form validation:
我使用 jQuery Validation ( PLUGIN URL) 插件进行大部分表单验证:
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:
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
- 最小和最大长度将空间计为一个字符
- 如果使用
number = true
和空格,它将返回错误,因为空格不是数字
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).
有什么解决方法吗?你们中有人遇到过同样的问题吗?请注意,我想继续允许输入空格(为了可读性)。
采纳答案by Soufiane Hassou
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
您可以添加一个 beforeValidation 回调来发送没有空格的值,请看这里:Jquery Validator before Validation callback,它似乎是您要找的
回答by Adam
a very smiple solution is :
一个非常简单的解决方案是:
replace the type of the phone input as the following :
将电话输入的类型替换为以下内容:
<input id="phone" type="number" maxlength="10" size="2" max="99" style="width:36px;" required />
and you would never have issues with spaces and stuff, just used that in my code !
并且您永远不会遇到空格和东西的问题,只需在我的代码中使用它!
enjoy
请享用