开发者

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 Elemen开发者_如何转开发ts 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.


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
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜