javascript jquery regex for validate a URL - javascript

I'm using following regex for validate a url,
jQuery.validator.addMethod("url_validation", function (value, element) {
return this.optional(element) || /^(([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");
This regex working fine. But when I validate http://www.alfaromeo.com/com/#/home , it says the url is invalid. When I enable #, the regex fail to validate some validations. So, can anyone give a suggestion to modify this regex to validate #, which is inside a url.
I refered regular expressions how to validate a URL to get this regex.
Thanx.

^(([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 changed
(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)
to
(\/([#-+_~.\d\w]|%[a-fA-f\d]{2,2})*)
Even so, don't expect it to work on every URL you might throw at it...

Related

URL validation using Javascript doesn't work well

I use the following validation for url:
jQuery.validator.addMethod("urlValidatorJS", function (value, element) {
var RegExp = (/^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/);
return this.optional(element) || RegExp.test(value);
}, "The url is incorrect");
For some reason the following invalid url is valid according the validation above:
http://www.example.com/ %20here.html (url with space).
How can I fix the validation?
(\/.*)?
You said the (optional) last thing in the URL is a slash followed by any number of any character.
Be more specific than . if you don't want to allow spaces.
At first: there already is a validation module from jQuery which you could use.
This would be your regex to match the URLs you mentioned (don't miss the i flag in the end!):
/^(https?:\/\/)(?:[A-Za-z0-9]+([\-\.][A-Za-z0-9]+)*\.)+[A-Za-z]{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/i
You were missing that there can also be other adresses like http://some.other.example.com. Also as mentioned in the other answer your identifier . was too greedy. I replaced it with \Sfor any non-whitespace character. You also had some unnecessary identifiers like {1} and your additional HTTP in the first part of your regex.
You can also use pages like regex101 to examine your regex and try it on different strings. It also explains each part of your regex so you can debug it better.

URL regex passed by regex buddy but failed by Dart

I have the following regex in JavaScript regex
(https?|ftp)://([-A-Z0-9.]+)(/[-A-Z0-9+&##/%=~_|!:,.;]*)?(\?[A-Z0-9+&##/%=~_|!:‌​,.;]*)?
It attempts to validate and empty space/s or a URL.
Yet when I attempt to use it in Dart RegExp
that uses a Perle flavour regex, it does not validates.
Any help is appreciated.
Your pattern doesn't look for lowercase characters. Either you add a-z to the respective character groups or you use caseSenstivie: false as shown in the code.
var urlPattern = r"(https?|ftp)://([-A-Z0-9.]+)(/[-A-Z0-9+&##/%=~_|!:,.;]*)?(\?[A-Z0-9+&##/%=~_|!:‌​,.;]*)?";
var result = new RegExp(urlPattern, caseSensitive: false).firstMatch('https://www.google.com');
If the result is != null a match was found.
Your pattern doesn't find http: URLs (only https or ftp) neither www.google.com.
Your statement about 'empty space' might apply to your email regexp you had in your question originally but not to your URL regexp you added in your comment.

Preventing users to enter email in the textarea

I've been using jquery validation plugin to validate my textarea doesn't contain email addresses.
I have a custom validator defined :
email = function() {
$.validator.addMethod("email", function(value, element) {
return this.optional(element) || !(value.match(/((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([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])))\.?/i));
}, emailValidationFailedMessage);
}
And it worked well. However if users put any of these :
(#)
[#]
{#}
|#|
instead of #, the email custom validator will not figure this. How can I edit this regex to add support for those as well?
Or is there a simpler way of doing this?
You can edit your expression and replace # with
(\[#\]|#|\{#\}|\(#\)|\|#\|\(#\))
or
([\[\{\|\(]?#[\]\}\|\)]?)
Both options do the same thing. The second is just a little shorter.
This will cause your validator catch any addresses that include #, [#], {#}, |#|, (#).
mail#mail.com
mail[#]mail.com
mail{#}mail.com
mail|#|mail.com
mail(#)mail.com
You could also try to edit the validator to strip the {}[]|| characters surrounding the # character in 'value' before you run the match.

Jquery validate plugin, no url in form field

I am having problem with creating a rule that will not allow url anywhere in form field. With little modification of "url" rule, i can prevent entering ONLY url with no text around, like:
this is http://google.com allowed
this is not
http://google.com
code:
jQuery.validator.addMethod("no_url", function(value, element, param) {
return this.optional(element) || !(/^(https?|ftp):\/\/(((([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));
}, jQuery.validator.messages.url);
I would like to be able to block first example, if possible, but i lack a skills to do that, so any help is more than welcome.
Try removing the ^ at the beginning and the $ at the end of your regex expression. These identify the beginning and end of the test expression.

regular expression with if statements

I have this as my regular expression:
var email = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
And this is my if statement:
if($('#email').val() ==""){
$('#emailErrorMsg').text("Please enter a valid email address.");
}
else if(!email.test('#email')) {
$('#emailErrorMsg').text("OK");
}
else($('#emailErrorMsg').text("Please enter a valid email address."));
});
When I type in a valid email address it says "OK". However, if I enter just some text for example it still says "OK" when I want it to say "Please enter a valid email address". Anyone any idea. By the way, I'm still an amatuer at this stuff!
The main problem is that you have a ? at the end of the regex, following parentheses that enclose the entire pattern. This effectively makes the entire match optional, so the regex will literally match anything.
Note also that you are testing the literal string #email, not the value of the #email element. Make sure you pass the appropriate string to test().
I see that you have jquery tag, so take a look to JQuery validate plugin, it will be better than a simple regex.
But if you still want regex, see Validate email address in JavaScript?
Validating emails is hard. The fully correct regex is a true monstrosity that you can see (if you dare) at http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html which probably isn't what you want.
Instead, you have a few options. Use a regex that matches 99% of emails, do it server side with an email validation library, or implement a finite state machine to parse it correctly. The state machine is probably too bulky (although allows neat stuff like suggestions for possible typos) and doing it all server side -- which you better be doing anyway (what if someone has JavaScript disabled?) -- loses the benefits of as-you-type checking.
That leaves a simpler regex that doesn't match all legal emails, but matches enough that the chances of someone registering with one that it doesn't are really slim.
The regex from Validate email address in JavaScript? should do the trick pretty well:
/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\
".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Also, you made a small typo:
else if(!email.test('#email')) {
$('#emailErrorMsg').text("OK");
}
is testing against the string '#email' -- not the element with the ID 'email'. Change that to:
else if(!email.test($('#email').val())) {
$('#emailErrorMsg').text("OK");
}
There's a little typo in your regex. Try this:
var email = /^([\w-\.]+)#([\w-]+\.)+[\w-]{2,6}?$/;
That should also handle the .museum case

Categories