How to use jQueryValidation without using name attributes

2.4k Views Asked by At

I'm using jQueryValidation and I would like to validate my form without needing to use name attributes.

If the form elements don't have the name attribute only the first element is validated.

<!DOCTYPE html>
<html>
    <head>
        <title>jQuery Validation Demo</title>
        <script src="jquery.js" type="text/javascript"></script>
        <script src="jquery.validate.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#loginButton').click(function(){
                $('form').validate();
            });
        });
        </script>
    </head>
    <body>
        <form action="#" id="loginForm">
            <input type="text" id="userText" class="required" placeholder="User" />
            <input type="password" id="passwordText" class="required" placeholder="Password" />
            <input type="submit" id="loginButton" value="Login" />
        </form>
    </body>
</html>

Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

You have to use name attribute in order to use class="required"

1
On

Try like this.

   $("#loginForm").validate({

                rules: {
                      userText:"required"
                       }
                     });