I am using the jQuery.validation plugin to validate a form. There is an optional field on the form called ‘survey_url’. If this field is blank I don’t want it validated, however if there is a value I want to make sure it is a valid URL.
I have the following rules, but despite the fact I have not set survey_url to ‘required’, it is flagged as an invalid URL even when it has no value:
//form validation rules
form.validate({
errorClass: "help-inline",
rules: {
"webcast[title]": "required",
"webcast[survey_url]":
{
url: true
}
},
messages: {
"webcast[title]":
{
required: "Please enter a title for this Webcast."
},
"webcast[survey_url]":
{
required:"You must enter a valid URL, or leave blank."
}
},
submitHandler: function(form) {
form.submit();
}
});
What should my rules look like so that survey_url is only validated as a URL if it has a value?
There must be something else going on in your script, because with what you’ve given, it works as expected: http://jsfiddle.net/ryleyb/ztDwh/1/
You should be able to submit that form with just the title filled, and nothing in the URL. If you put in a URL, it must be in a correct URL format. Sounds like what you want?