1

我有一个使用http://digitalbush.com/projects/masked-input-plugin/屏蔽并使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/验证的电话号码。

代码位于下方,“几乎”工作示例位于http://jsfiddle.net/dnv38/

请注意,电话号码不是必需的,但两个插件冲突导致需要电话号码。不久前,我以为我找到了每个jquery.maskedinput-1.3.js 与 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>
4

1 回答 1

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);
        }
    },
于 2013-08-08T17:54:28.860 回答