2

I'm trying to use the JQuery validator plugin to validate a rails app but it's neither throwing any errors or validating anything. I don't know what else is wrong with my code--any help would be appreciated!

$(document).ready(function() {
$(".theform").validate({
    rules: {
        name: {
            required:true,
            minlength:2,
            maxlength:50
        },
        email: {
            required: true,
            email: true,
            minlength:2,
            maxlength:50
        },
        phone_number: {
            required: true,
            phoneUS: true,
            minlength:9,
            maxlength:20
        }
    },
    messages: {
        name: {
            required: "Please provide your name",
            minlength: "Too few characters!",
            maxlength: "Too many characters!"
        },
        email: {
            required: "Please provide your email",
            email: "Your email address must be in the format of [email protected]",
            minlength: "Too few characters!",
            maxlength: "Too many characters!"
        },
        phone_number: {
            required: "Please provide your phone number",
            phoneUS: "Please provide a valid US phone number",
            minlength: "Too few characters!",
            maxlength: "Too many characters!"
        }
    }
});
$("input#ajax").click(function() {
    $.ajax({
        type: "POST",
        url: "/create_user",
        data: {name: $('#name').val(), email: $('#email').val(), number: $('#phone_number').val(), ajax:"true"},
        success: function(msg){
            //console.log(msg.name);
            $("div#n").html(msg.name);
            $("div#e").html(msg.email);
            $("div#p").html(msg.phone_number);
        }
    });
});
});

EDIT: here's the form I'm trying to validate:

<div id="theform">
<form name="form" action="create" method="post">

    Name: <input type="text" name="name" id="name"/><br/>
    Email: <input type="text" name="email" id="email"/><br/>
    Phone Number: <input type="text" name="phone_number" id="phone_number" /><br/>
    <input type="submit" value="Normal Submit" id="normal">
    <input type="button" value="Ajax Submit" id="ajax">

</form>
</div>
2
  • 1
    Can you show your generated HTML? Commented Jun 25, 2012 at 16:30
  • I just did! Check the edit above :) Commented Jun 25, 2012 at 16:46

1 Answer 1

1

Your selector is wrong:

$(".theform").validate({ ... });

selects all elements with class theform, which none of your elements have. You need to select the form element itself:

$("#theform > form").validate({ ... });

or

$("form[name='form']").validate({ ... });

Example: http://jsfiddle.net/nQq6t/

Sign up to request clarification or add additional context in comments.

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.