0

I have created a regex that seems to be quite stable in validating normal Http URLs, however, it seems to be allowing spaces to be entered anywhere but the protocol.

So, http://dango me/mypage is being allowed.

'is-url': function() {
    return /^(https?:\/\/[a-zA-Z0-9_+%-]+(.[a-zA-Z0-9+\_%-]+)*(:[0-9]{1,5})?(\/[a-zA-Z0-9+()?#~=&\._%-]*)*)?$/.test(this);
}
Kana Ki
  • 382
  • 2
  • 15

3 Answers3

4

Escape the dot:

return /^(https?:\/\/[a-zA-Z0-9_+%-]+(\.[a-zA-Z0-9+\_%-]+)*(:[0-9]{1,5})?(\/[a-zA-Z0-9+()?#~=&\._%-]*)*)?$/.test(this);
//                             here __^

without escaping it matches any character but newline so, of course, a space.

Toto
  • 86,179
  • 61
  • 85
  • 118
0
> return /^(https?:\/\/[a-zA-Z0-9_+%-]+(.[a-z

I guess it is because you use . (dor meaning anythisn including space it probably have to escape it and should be

 return /^(https?:\/\/[a-zA-Z0-9_+%-]+(\.[a-z
i100
  • 4,313
  • 1
  • 20
  • 20
0

You should escape the dot. . means 'match any character except newline' in regular expressions. If you want a literal dot, escape it like so \..

'is-url': function() {
    return /^(https?:\/\/[a-zA-Z0-9_+%-]+(\.[a-zA-Z0-9+\_%-]+)*(:[0-9]{1,5})?(\/[a-zA-Z0-9+()?#~=&\._%-]*)*)?$/.test(this);
}
SQB
  • 3,770
  • 2
  • 26
  • 45