-1

I want to check if a textbox contains a valid URL using regular expression. A valid url can be as follows.

http://www.google.com
https://www.google.com
http://clrs.mlslist.com:6103/
http://clrs.mlslist.com:6103/premium/login
http://reso.mlslisting.com/premium/login?rets-version=rets/1.5

Can someone help me to frame the validation expression for the ASP RegularExpressionValidator?

Vishnu Y
  • 2,211
  • 4
  • 25
  • 38
  • Aside from obviously not using any searches - what did You try? Regex is not a suitable tool to validate URLs - http://stackoverflow.com/questions/827557/how-do-you-validate-a-url-with-a-regular-expression-in-python (You can invent a subset-regex to match a subset of RFC for Your temporary needs, but You'll stuck with improving regex to fit more and more into it over time, which isn't the right approach) – Vlad Nov 26 '13 at 10:32

2 Answers2

2

From https://urlregex.com/:

/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/

This is a pretty lenient regex that should suit your needs:

^https?://[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.\w+(:\d+)?(/[\w-=.?]+)*/?$

Requires http:// or https://

Requires a set of characters, followed optionally by more sets of characters separated by dots (e.g. google or www.google or img1.srv2.google).

Requires another . followed by a set of characters (e.g. .com).

Allows optional port (e.g. :8080)

From there allows sets of word/number characters and symbols -=.? separated by /s (e.g. /premium/login?rets-version=rets/1.5)

RegExr Example

OGHaza
  • 4,795
  • 7
  • 23
  • 29
  • Honestly though, this has been posted to death. – OGHaza Nov 26 '13 at 10:36
  • it is not a good practice to try to use regex for this. You'll get Yourself into long loop of extending (fixing and improving) the regex - once it'll be polished to perfection (and occupy 5 screens of text) - You'll find it quite inefficient ;) Just few examples: `http://localhost:80/index.html` `http://aa.bb:0/` `http://a._:99999/`- list is endless ;) – Vlad Nov 26 '13 at 10:46
  • Well it matches the 2nd of those examples, the 3rd is almost certainly an invalid url. `._` can't be a real top level domain. and it only doesn't match a localhost address because I intentionally made it more complex so that it would not. If I was trying to extract URLs from a log file I would use regex as it is a simple solution. – OGHaza Nov 26 '13 at 10:57
  • @OGHaza Thanks. Then suppose I want to make the http or https portion optional, how the expression would change? – Vishnu Y Nov 26 '13 at 11:58
  • Change `^https?://` to `^(https?://)?` – OGHaza Nov 26 '13 at 12:02
  • @OGHaza how can I make the http or https portion case insensitive? The URL should be valid for Http, HttP, HTTPS, etc. – Vishnu Y Dec 05 '13 at 12:10
  • Hey Vishnu if you want only the https to be case insensitive you could use something a little strange like: `^[hH][tT][tT][pP][sS]?://` to allow any cases like `HtTpS`. However if you're fine with the whole URL being case insensitive you can use a flag: If your code looks like `string.match(/yourregex/)` you can add an `i` and the last `/` > `string.match(/yourregex/i)`. If your code looks like `re = new RegExp('yourregex')` you can use `i` as the second parameter `re = new RegExp('yourregex', 'i')` – OGHaza Dec 05 '13 at 12:19
  • @OGHaza Noticed an issue. Its allowing multiple ? at a stretch like http://www.reos.com/upload.aspx??val=1 And its not allowing multiple parameters such as upload.aspx?val1=5&val2=7 – Vishnu Y Dec 05 '13 at 13:34
  • The multiple parameter problem can be fixed by adding `&` to the last character class `[\w-=.?&]`. The `?` problem is because this regex doesn't really validate a URL, it just checks for something that looks mostly like a URL <- there are TONS of invalidly formed URLs it'll match. You can try [something like this](http://regexr.com?37guo) but really after this point taking the advice from Vlad's comment is the best idea. – OGHaza Dec 05 '13 at 14:30
  • On the [question](http://stackoverflow.com/questions/827557/how-do-you-validate-a-url-with-a-regular-expression-in-python) vlad linked you can see an example of the sort of regex required to accurately validate a URL - it's unbelievably long – OGHaza Dec 05 '13 at 14:35
  • Not valid for 'Https://192.160.96.194:8083' and same patterns – Kanwarpreet Singh Feb 12 '20 at 10:07
  • @KanwarpreetSingh, very true. Something better here https://urlregex.com/ – OGHaza Feb 12 '20 at 23:37
  • @OGHaza I resolved the issue in a bit different manner. I already had validations for URL and add one more condition to it. URL url = new URL(strBaseurl); int port = url.getPort(); if (port > 65535) { Toast.makeText(context, context.getResources().getString(R.string.entered_url_not_valid), Toast.LENGTH_SHORT).show(); return; } – Kanwarpreet Singh Feb 17 '20 at 06:51
0

Use aRegularExpressionValidator and set the expression to something like this:

^([\S\s]{0,4})$

Replace 4 with your desired length (max)

Example:

<asp:TextBox id="txtbx" Runat="server" />

<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" 
        ErrorMessage="Error: RegularExpressionValidator" 
        ValidationExpression="^([\S\s]{0,4})$" 
        ControlToValidate="txtbx" />

<asp:Button ID="Button1" runat="server" Text="Button" />
Vinayak Pahalwan
  • 2,915
  • 4
  • 26
  • 34
  • Not working. The expression is simply validating whatever I type in the textbox. – Vishnu Y Nov 26 '13 at 10:09
  • @Vinyak all your regex does is check the length. Also it allows line breaks which it definitely should `.{0,4}` would be more suitable, but still unsuitable of course. – OGHaza Nov 26 '13 at 10:20