我有一个使用http://digitalbush.com/projects/masked-input-plugin/屏蔽并使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/验证的电话号码。
代码位于下方,“几乎”工作示例位于http://jsfiddle.net/dnv38/。
请注意,电话号码不是必需的,但两个插件冲突导致需要拨打电话号码。前一阵子,我以为我找到了jquery.maskedinput-1.3.js conflicts with jquery.validate 1.9.0的答案,但事实证明这不是正确的。
如何屏蔽输入并对其进行验证,但不是必需的?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>Validate</title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.js" type="text/javascript"></script>
<script src="http://snort-rule-manager.googlecode.com/svn-history/r2/trunk/assets/9982dce2/jquery.maskedinput.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#phone").mask("(999) 999-9999");
var validator=$("#form").validate({
rules: {phone:"phoneUS"},
submitHandler: function(form) {
alert('submitHandler');
}
});
});
</script>
</head>
<body>
<form id="form" method="post">
<input type="text" name="phone" id="phone" value="" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
答案 0 :(得分:1)
我能够通过使用闭包修改验证插件的onfocusout方法来解决这个问题,但这样做似乎是错误的....
onfocusout: function (element)
{
if (!this.checkable(element) && (element.name in this.submitted || !this.optional(element)))
{
var currentObj = this;
var currentElement = element;
var delay = function () { currentObj.element(currentElement); };
setTimeout(delay, 0);
}
},