2

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.

4
  • 1
    Surely the name attributes are useful for handling the data? What's your problem with using them? Commented Apr 13, 2011 at 9:53
  • 1
    Have you tried validating an element like this docs.jquery.com/Plugins/Validation/Validator/element#element? Commented Apr 13, 2011 at 9:56
  • @Calum none, I just wasn't properly understanding the differences between them. Commented Jun 4, 2011 at 21:17
  • 1
    @Calum When using Stripe we don't ever want to pass data to the server, so not using a name attribute on the inputs makes sense. Instead, we use a data attribute so Stripe.js can pick them up. Commented May 13, 2015 at 8:37

2 Answers 2

2

Try like this.

   $("#loginForm").validate({

                rules: {
                      userText:"required"
                       }
                     });
Sign up to request clarification or add additional context in comments.

1 Comment

OP said he wants to validate elements by ID instead of name
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.