Asle
Asle

Reputation: 827

jQuery validate problems - not working - form just submits

I have read everything I could find here on this subject. I think the problem is simple and easy but I can not find out. I would be glad for some rested eyes to take a quick look. What am I doing wrong here? This simple form just submits and does not validate. My problem is in a bigger form but I found out I should test it simple first. I even tested on jsfiddle and do not understand the response there either http://jsfiddle.net/asle/fmYLP/

<!doctype html>
<html lang="us">
<head>
    <meta charset="utf-8">
    <title>jQuery Validate Example Page</title>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js'></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script>
    <script type="text/javascript">
    jQuery().ready(function(){
        $("#testform").validate();
    })
    </script>
</head>
<body>
<form id="testform" name="testform" method="post">Name:
    <input name="testname" type="text" />
    <br />Adress:
    <input name="testadress" type="text" />
    <br />
    <input type="submit" value="submit">
    <br />
</form>
</body>
</html>

Upvotes: 1

Views: 14755

Answers (2)

Sparky
Sparky

Reputation: 98738

According to the docs, your way is "not recommended": jQuery().ready(function(){...}).

Instead use jQuery(document).ready(function(){...})

or jQuery(function(){...})


Quote OP:

This simple form just submits and does not validate.

Because you have not declared any validation rules, so the form is always valid no matter what kind of data you enter, or with no data at all.

Quote OP's Comment:

I tried to add "required" to the input and it works. Do I always have to add "required" to every field? It is not like that in the demos. <input required name="testname" type="text" />

I'm not sure which demos you're talking about, but you must declare validation rules someplace, otherwise, how do you expect the plugin to know which fields are required or apply another kind of rule?

Here are three valid examples...

By inline attribute (HTML 5):

<input required="required" name="testname" type="text" />

or by class:

<input class="required" name="testname" type="text" />

or via .validate() in jQuery:

$(document).ready(function(){

    $("#testform").validate({
        rules: {
            testname: {
                required: true,
                // another rule
            },
            testaddress: {
                required: true,
                digits: true
            }
        }
    });

});

Working DEMO: http://jsfiddle.net/4HSYz/


There are also methods available for manipulating the rules dynamically.

See the rules('add') method.


Read the full documentation: http://docs.jquery.com/Plugins/Validation

Upvotes: 3

Tamil Selvan C
Tamil Selvan C

Reputation: 20209

Try

jQuery(document).ready(function() {
   $('#testform').validate();
});

Upvotes: 0

Related Questions