J'ai 2 addMethods appliqués à un champ de saisie.
Note : J'ai besoin de 2 messages d'erreur comme empty error et valid url si l'un d'entre eux est invalide dans un addMethod.
1 - Vérification requise 2 - Vérifiez la validation de l'URL.
Y a-t-il un moyen de fusionner les deux et d'obtenir le même résultat ?
jQuery.validator.addMethod("complete_req", function (val, elem) {
var value = $.trim($("#linkTypeDd").val());
if (value == "2")
{
if (val.length == 0) {
return false;
}
return true;
}
},"");
jQuery.validator.addMethod("complete_url", function (val, elem) {
var value = $.trim($("#linkTypeDd").val());
if (value == "2") {
// if no url, don't do anything
if (val.length == 0) {return true;}
// if user has not entered http:// https:// or ftp:// assume they mean http://
if (!/^(https?|ftp):\/\//i.test(val)) {
val = 'http://' + val; // set both the value
$(elem).val(val); // also update the form element
}
// now check if valid url
// http://docs.jquery.com/Plugins/Validation/Methods/url
return "Suppose this is url rejex";
}
return true;
});
jQuery.validator.messages.required = "";
var validator = $("#footer-form").validate({
debug: true,
submitHandler: function (form) {
$(form).ajaxSubmit(formoptions);
},
invalidHandler: function (e, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1 ? 'You missed 1 field. It has been highlighted.' : 'You missed ' + errors + ' fields. They have been highlighted.';
showMessage("#error", message);
$("#success").hide();
}
else {
$("#error").hide();
}
},
rules:
{
DisplayName: "required",
UrlName: "required",
ExternalPath: {
complete_req : true,
url: "complete_url"
}
},
errorClass: "invalid",
validClass: "valid",
errorContainer: "#error"
});