I want to avoid user from submitting website URLs in text-area using (JQuery) client sid开发者_JS百科e validation. I am using Validation plug-in.
Example:
http://www.example.com
I need to validate when user types http:// or www in text-area
You could use a custom function with a matching regex like this:
$.validator.addMethod('no_url', function validatePositionNummer(value){
var re = /^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$/;
var trimmed = trim(value);
if( trimmed == ''){
return true;
}
return trimmed.match(re);
},"No URLs allowed!");
Then you just add your new custom validation method to the element:
$("#your_form").validate({
textarea: no_url
});
You would have to fine-tune the regex ofcourse.
URLs or Links? There is a difference.
However, in either case, don't forget to check on the backend too. You can't trust client side validation. It should only be used to make the user's life easier.
@BernardMarx Thanks for the solution.
Here i used extra regex to validate protocols according to my requirement. Now i need to validate at the end. suggestion please..
For Example:
http://www.example.com/index.php
http://www.example.php/home/
$.validator.addMethod('no_url', function validatePositionNummer(value){
var re = /^[a-zA-Z0-9\-\.\:\\]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$/;
var re1 = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
var trimmed = $.trim(value);
if( trimmed == '' ){
return true;
}
if( trimmed.match(re) == null && re1.test(trimmed) == false){
return true;
}
},"No URLs allowed!");
精彩评论