How do I do client-side form validation in jQuery?

Posted by marcamillion on Stack Overflow See other posts from Stack Overflow or by marcamillion
Published on 2011-01-05T07:49:08Z Indexed on 2011/01/05 7:53 UTC
Read the original article Hit count: 204

Filed under:
|

I am trying to use this plugin: http://docs.jquery.com/Plugins/Validation

Where #user_new is the id for my form, this is what my code looks like:

$('#user_new').validate({
    rules: {
        user[username]: "required",
        user[email]: {
            required: true,
            email: true
        }
    },
    messages: {
        user[username]: "Please specify your name",
        user[email]: {
            required: "We need your email address to contact you",
            email: "Your email address must be in the format of [email protected]"
        }
    }
})

Where these are how my input fields look when the page is rendered (generated by Rails):

<input class="clearField curved" id="user_f_name" name="user[f_name]" size="30" type="text" value="First Name" /><br /> 
        <input class="clearField curved" id="user_l_name" name="user[l_name]" size="30" type="text" value="Last Name" /><br /> 
        <input class="clearField curved" id="user_username" name="user[username]" size="30" type="text" value="Username" /><br /> 
        <input class="clearField curved" id="user_password" name="user[password]" size="30" type="password" value="Password" /><br /> 
        <input class="clearField curved" id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" value="Password" /><br /> 
        <input class="clearField curved" id="user_email" name="user[email]" size="30" type="text" value="Email Address" /><br /> 

I was just trying to validate username & email first. Then take it from there.

For the life of me, I can't figure out how to specify the syntax and the rules for working with this plugin.

Help!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-validate