jquery 验证适用于某些浏览器,但不适用于其他浏览器
为什么这对某些人有效,而对其他人无效?其他人只是点击按钮,不起作用。在我添加 nospaces 验证方法后它就开始这样做了。任何建议将不胜感激。
<form method="post" action="#" id="client_form" class="order-form">
<input type="text" id="user_name" name="user_name" class="req nospaces" />
</form>
<button id="client_next_btn"><?php echo($content->getString('order')); ?></button>
<script type="text/javascript">
$(function ()
{
jQuery.validator.addClassRules('req', { required: function (el) { return $(el).is(':visible') } });
var nums = ['0','1','2','3','4','5','6','7','8','9'];
var letters = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'];
var alphanumeric = nums.concat(letters);
jQuery.validator.addMethod("nospaces", function(value, element) {
var val = value.split("");
var bool = true;
for (var i in val)
{
if (jQuery.inArray(val[i], alphanumeric) == -1)
{
bool = false;
break;
}
}
return bool;
}, "* <?php echo ($content->getString('no_spaces')); ?>");
$('#client_form').validate();
$('#client_next_btn').click(function ()
{
$('#client_message').empty();
if ($('#client_form').validate().form())
{
alert('works');
}
}
}
Why does this work for some people, and doesn't for others? Others just click the button and it doesn't work. It started doing that after I added the nospaces validation method. Any suggestions would be very much appreciated.
<form method="post" action="#" id="client_form" class="order-form">
<input type="text" id="user_name" name="user_name" class="req nospaces" />
</form>
<button id="client_next_btn"><?php echo($content->getString('order')); ?></button>
<script type="text/javascript">
$(function ()
{
jQuery.validator.addClassRules('req', { required: function (el) { return $(el).is(':visible') } });
var nums = ['0','1','2','3','4','5','6','7','8','9'];
var letters = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'];
var alphanumeric = nums.concat(letters);
jQuery.validator.addMethod("nospaces", function(value, element) {
var val = value.split("");
var bool = true;
for (var i in val)
{
if (jQuery.inArray(val[i], alphanumeric) == -1)
{
bool = false;
break;
}
}
return bool;
}, "* <?php echo ($content->getString('no_spaces')); ?>");
$('#client_form').validate();
$('#client_next_btn').click(function ()
{
$('#client_message').empty();
if ($('#client_form').validate().form())
{
alert('works');
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为提示,您可以摆脱这个:
并使用内置的
required
类,只需添加ignore
选项到您的.validate()
调用排除:hidden
元素,如下所示:Just as a tip, you can get rid of this:
and use the built in
required
class, just add theignore
option to your.validate()
call to exclude:hidden
elements, like this:就在我发布问题后,我找到了答案 - addMethod 必须与 addClassRules 一起使用,而不是单独使用
Just after I posted the question, I figured out the answer - addMethod has to go together with addClassRules and not by itself