Jquery validate plugin, no url in form field - javascript

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.

Related

How do you automatically hyperlink text after the symbol #?

On my site, there is text that may contain a mention to an account, like #Melon or #Banana. How can I use JavaScript to automatically link these to their respective account (e.g. #Banana -> example.com/users/Banana)? I also want to make sure that if someone wrote something like "#Banana's", it would only link "#Banana". Please comment if this is confusing.
Thanks!
Here you go, with regex but it basically depends on how a user's username could look like
const regex = /#([\w\d_-]*)(?:[^\.'<\bbr\b/*>\s])*/g;
const testCases = ["#Banana<br/>'s", "#Fruit", "#MOnkeys-12342<br>", "#International_Alien234"];
console.log(testCases.map(str => ({original: str, replace: str.replaceAll(regex, '$&')})));
you can regex them out:
text.replace(/(^|\s)#([^\s]+)(\s|$)/g, "$1#$2$3")
for the case #Banana's, you'll need to define which chars are part of the link, and which chars terminate the #statement.
This regex assumes the link starts with # and ends with a whitespace.

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.

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.

regular expression to block anchor tags in comments textarea

Can anyone tell me please how to write a regular expression in Java or Javascript that will return false if "href", "" "www." or "http" is entered into the text area? I don't want people or bots to be able to enter those terms into the text area?
I tried (?![http|<[^>]*>|href|www\.])[A-Za-z] but its not working.
What you could try is:
/(http|www.|href)/g
the global option causes a regex to not stop at the first match.
try practicing with:
http://scriptular.com/#%28href|www.|http%29||||g||||[%22\%22href\%22%2C%20\%22\%22%20\%22www.\%22%20or%20\%22http%22]
(http://scriptular.com/)
Also, note that when you use:
[http|<[^>]*>|href|www\.]
keep in mind that brackets signify a matching or, that is,
[http|www|href] would do the same thing as [efhptrw|], i believe.
(unless pipe works in brackets, too - can anyone clarify that?)
EDIT:
What about:
/(http[s]?:\/\/|www\.\S|href)/i
try it on this kind of test string:
"hrf", "hre"
"ww."
or "htp
"hrf", "href"
"www."
"www. "
or "http://
or "https://
http
Note that it will return nothing at all until the match has been completely correctly entered.
I don't think you need negative lookahead on this one.
var reg = new RegExp(/(http|www.|href)/, 'g')
console.log( yourInput.value.test(reg) ); //prints true if there is http or www. or href

javascript jquery regex for validate a URL

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...

Categories