-1

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
  • 390
  • 2
  • 16

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
  • 89,455
  • 62
  • 89
  • 125
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,529
  • 1
  • 22
  • 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,926
  • 2
  • 28
  • 49