0

How to modify the following regex pattern to make it without requiring http or https:

<input type="url" 
 pattern="^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\:\'\/\\\+=&amp;%\$#_]*)$"
 validationMessage="Enter a valid web url />

User just type into normal www. is ok, not require http:// or https://.

I tried following, and typed into www.google.com, but not working.

pattern="^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\:\'\/\\\+=&amp;%\$#_]*)$"
4

2 回答 2

0

pattern="^(?:(ht|f)tp(s?)\:\/\/|\/\/)?[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\:\'\/\\\+=&amp;%\$#_]*)$"

这将让你做例如:

http://www.mysite.com
//www.mysite.com
www.mysite.com
于 2013-08-24T16:43:56.893 回答
0

你可以试试这个:

^((ht|f)tp(s?)\:\/\/)?[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\:\'\/\\\+=&amp;%\$#_]*)$

这将允许输入如下:

http://www.google.com
www.google.com
https://www.google.com
google.com

并且不会允许以下内容:

//www.google.com

希望这就是你要找的。

于 2013-08-24T18:35:50.397 回答