JQuery Validator "required" does not work when value is set to statup

I have a problem with jQuery Validator. I want to use the "required" property to enter text. It does not work when input sets a value attribute using HTML code (tested on Firefox (3.5), and on IE 8 it works a bit better on IE).

History: 1. Page loading; 2. The value is cleared; 3. focus is changed. 4. Nothing happens, but an error message should be displayed; 5. Return to the field and enter a few characters. 6. change of focus; 7. return to the field; 8. Cleaning the field. 9. An error is issued even before leaving the field.

HTML code:

<!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>
    <script src="Web/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="Web/Scripts/jquery.validate.js" type="text/javascript"></script>    
</head>
<body>
    <form id="form1">
        <input type="text" id="name1" name="name1" value="test" /><br />
        <input type="text" />
    </form>

    <script type="text/javascript">
        $(document).ready(function() {
            var validator = $("form").validate({

                rules: {
                    name1: {
                        required: true,
                        minlength: 2
                    }
                },
                messages: {
                     name1: "bad name"
                },
            });
        });
    </script>
</body>
</html>
+3
source share
3 answers

onfocusout, , ( ):

$(function() {
    var validator = $("form").validate({
        rules: { name1: { required: true, minlength: 2 } },
        messages: { name1: "bad name" },
        onfocusout: function(element) { $(element).valid(); }
    });
});

(, ) onfocusout, .

+7

, , Michel:

var validator = $('form').validate();

validator.settings.onfocusout = function(element) {
    $(element).valid();
};
/*validator.settings.onkeyup = function (element) {
    $(element).valid();
};*/
+1

you forgot to say what validation was the message: try the following:

 messages: { 
             name1:
                   {
                      required : "This is a required field",
                     minlength: "Please enter 2 chars or more"
                   }
            }

Michelle

0
source

Source: https://habr.com/ru/post/1729706/


All Articles