jquery - Configure JQ Validate to accept URL without http:// prefix -
i using jquery validate , need custom validate method.
the default url method not accept url unless has schema. users ever providing url's begin http://, i'd rather not have put them through hassle of typing each time.
i trying create validate method use default url method check validity of url. if fails, want prepend http:// , run again. here's have far:
addcustomurlvalidator: function () { $.validator.addmethod("short-url", function(value, element) { if (value.length == 0) return true; // field optional // attempt validate default url method. return true on valid. // ... // prepend http:// value , attempt validation again. return true on valid. // ... return false }, jquery.validator.format("must valid url format")); }
i using jquery validate , need custom validate method.
see the .addmethod()
method documentation how below.
the default url method not accept url unless has schema. users ever providing url's begin
http://
, i'd rather not have put them through hassle of typing each time.
i've copied function default url
method inside plugin , edited regex don't need uri scheme. (i deleted (https?|s?ftp):\/\/
part of regex.) notice new rule called my_url
, , can change whatever want.
jquery.validator.addmethod('my_url', function( value, element ) { // contributed scott gonzalez: http://projects.scottsplayground.com/iri/ return this.optional( element ) || /^(((([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.)+(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\ue000-\uf8ff]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test( value ); }, 'please enter valid url without "http".');
if fails, want prepend
http://
, run again.
manipulating data should on server-side after form has been submitted. should return true
or false
custom method, not manipulate data, nor programmatically force same data through same rule multiple times. either user entered correct format or didn't... user tries again.
Comments
Post a Comment