1

I wrote a function to validate one input field in a form by jquery . but there are two additional text fields in my form . I am not sure a way to add tow additional rules to them . This is what i have tried so far .

$(document).ready(function () {
$(document).ready(function () {
jQuery.validator.addMethod("url_validation", function (value, element) {
return this.optional(element) || /^(([http|https|HTTPS|HTTP]+:\/\/))?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/i.test(value);
}, "error");

$('#myform').validate({
    /** this section is working */
    rules: {
        text_name: {
            required: true,
            url_validation: true,
            maxlength: 2000
        }
    },
    messages: {
        text_name: {
            required: "Please specify a URL",
            url_validation: "The domain you have entered is not a valid domain or sub domain name. Please try again.",
            maxlength: "You have exceeded the maximum length"
        }
    }
    /**********************************************************/
    /*this is where i tried to add other rules to two of my other textboxes*/
    rules: {
        text_name2: {
            required: true,
            url_validation: true,
            maxlength: 2000
        }
    },
    messages: {
        text_name2: {
            required: "Please specify a URL",
            url_validation: "The domain you have entered is not a valid domain or sub domain name. Please try again.",
            maxlength: "You have exceeded the maximum length"
        }
    }
    /*above thing is not working*/

});

In my HTML there are other two text boxes i need to validate . how to perform this task?

1 Answer 1

2

The validate plugin takes an options object with rules and messages as a parameter. Fill them up, but don't double specify the rules and messages objects.

It should be:

$('#myform').validate({
    rules: {
        text_name: {
           ...
        },
        text_name2: {
           ...
        }
    },
    messages: {
        text_name: {
           ...
        },
        text_name2: {
           ...
        }
    }
});
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.