开发者

$.validator.addMethod()

I have a dropdownlist

$.validator.addMethod("ddlNames", function (value) {
        if ($("#NameList.val()=="-1") 
        {
            return false;
        }
        else 
        {
            return true;
                }        

    }, "Please select a valid Name");

This validation is going fine. But when i change the dropd开发者_JAVA百科own value (valid selection ) the error message occured previously its stil displayed. Please can any one help me out?

thanks


You need to call the validation programmatically when changing the drop down. Eg. If you have: YOUR_FORM validation. Assign a variable to the validate method. And then call that with .form() on the change event of your dropdown.

Documentation: http://jqueryvalidation.org/Validator.form/

Sample code:

$('#ddlNames').change(function() {
    form_validator.form();
});

var form_validator = $("#YOUR_FORM").validate({
   ignore: "",
    rules: {
        firstname: {
            required: true,
            minlength: 2
        },
        ...


In document.ready you need to add another line that says (replace: # < targetform> with your form):

$('#<targetform>').validate({
    rules : {
        names : { ddlNames: true }
}
});

Make sure you have a class="required" on your <select>


You should not need to clear them yourself, but there are ways...:

$("#NameList").removeClass("error").addClass("valid");

or

$.validator.resetForm();

That will make sure you start off valid each time.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜