Reputation: 640
Hello i wrote a regular expression to validate URL
jQuery.validator.addMethod("url_validation", function (value, element) {
return this.optional(element) || /^([ \t+])?(([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")
I could not find a way to prevent entering anything after entering the domain : for example : - "http://www.mydomain.uk/whateverthing.ext" i want to stop entering the "whateverthing after entering the domain . I could not figure out a way to solve this issue . can anyone give me a help?"(this will include validating the URL as well)
Upvotes: 1
Views: 182
Reputation: 121810
OK, so you want two things:
You can proceed as such:
^https?://([a-z0-9-]+(?:\.[a-z0-9-]+)*)/?(.*)
;\.
.^[a-z0-9]+(-[a-z0-9]+)*$
: if one doesn't, invalid URL.And that doesn't take IP addresses in the hostname part...
I am surprised anyhow that jQuery doesn't have methods to check for that. Have you looked?
Edit: it has: http://docs.jquery.com/Plugins/Validation/Methods/url
Upvotes: 6