I am trying to validate a textbox to ensure that a URL is written inside (minus the "http://" part). Here is my code:
var isUrl;
var regex = new RegExp("^(?!http)(www\.)?(([a-z])+\.[a-z]{2,}(\.[a-z]{2,})?)");
var siteUrl = e.target.value;
if (siteUrl.match(regex)) {
isUrl = true;
}
else {
isUrl = false;
}
So my regular expression is ^(?!http)(www\.)?(([a-z])+\.[a-z]{2,}(\.[a-z]{2,})?).
I was under the impression that this would do the following:
- NOT match anything beginning with
http.. which it does correctly - Allow an optional
www.at the start - Accept 1 or more characters from a-z to be typed in
- Accept a dot after those characters
- Accept 2 or more characters following the dot, and
- Allow an optional dot followed by two or more characters again.
In practice, the textbox accepts strings like "ashdiguasdf" and "aksjdnfa:://',~" which it should not do. Can anyone explain why?
$anchor as well.var regex = /^(?!http)(www\.)?(([a-z])+\.[a-z]{2,}(\.[a-z]{2,})?)/i;(I think case-insensitive matching is necessary here, thus I added/i).