user1032531
user1032531

Reputation: 26281

jquery.validate conflicts with masked input

I have a phone number which is masked using http://digitalbush.com/projects/masked-input-plugin/ and validated using http://bassistance.de/jquery-plugins/jquery-plugin-validation/.

The code is located below, and an "almost" working example is located at http://jsfiddle.net/dnv38/.

Note that the phone number is not required, yet the two plugins conflict resulting in making the phone number required. A while back, I thought I found an answer per jquery.maskedinput-1.3.js conflicts with jquery.validate 1.9.0, but it turned out not to be correct.

How do I mask an input and validate it, but not make it required?

<!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>

Upvotes: 1

Views: 3342

Answers (1)

user2665463
user2665463

Reputation: 21

I was able to fix this by modifying the onfocusout method of the validation plugin with a closure, but it seemed wrong to do this....

    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);
        }
    },

Upvotes: 1

Related Questions