SoftwareSavant
SoftwareSavant

Reputation: 9737

Jquery Validation Basics

I asked a pretty tough question on Jquery Validation earlier on, and nobody seems able to answer it, so I am just going to go for the more basic how-to question... Say I have a form (MVC2 asp.net form might I add), one textbox, on textbox with DateTime in it and one radio button (the values of the Radio Button are Yes or No). How do I get simple custom validation on those Form Elements if I know the name of the form elements?

Keep in mind these elements are going to be generated Dynamically and so are there validation rules... I am for now just hoping to fudge it a little bit and put permanent validation in there...

I am a complete Jquery and Javascript nooob... All I have ever done with either programming language was add simple tools into my code...

Thank you for your help.

Upvotes: 1

Views: 250

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

How do I get simple custom validation on those Form Elements if I know the name of the form elements?

The documentation contains many examples of how to setup the plugin. Here's an example with static rules:

$('#myform').validate({
    rules: {
        Name: { 
            required: true
        },
        DateOfBirth: {
            required: true,
            date: true
        },
        IsMajor: {
            required: true
        }
    }
});

and if you wanted to dynamically add those rules you could do this:

$('#myform').validate();

and then dynamically add rules to individual form elements:

$('#Name').rules('add', {
    required: true
});

$('#DateOfBirth').rules('add', {
    required: true,
    date: true
});

$('#IsMajor').rules('add', {
    required: true
});

Upvotes: 1

Aaron D. Marasco
Aaron D. Marasco

Reputation: 6748

You don't need to know the id of the element as long as you assign it to the proper class, e.g. the built-in "required". Set the class and everything else is taken care of.

Upvotes: 0

Related Questions