MUS
MUS

Reputation: 1450

Problem in URL validation using ASP.Net Regular Expression Validator

I'm trying to use the ASP.Net Regular Expression Validator to validate a URL field. URL is www.tachibana.co.jp/tokyosys.htm. Validation expression used is ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" but this is not working. Is there anything wrong with the Regular expression or URL ?

Rules are as below.

  1. It should validate even if (http or https) is included or not.
  2. It should also trim the URL before validating.
  3. It should also validate the sub domain URL's
  4. It should also validate the URL's to a file on domain or sub domain.

thanks

Upvotes: 0

Views: 5287

Answers (4)

Chenthil
Chenthil

Reputation: 326

var re = /(http(s)?:\\)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/
if (re.test(txt)) {
alert('Valid URL')
}

you can add domain needed in the last field of com,in,org

Upvotes: 0

Black Cloud
Black Cloud

Reputation: 481

"(http(s)?://)?([\www]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?" 

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336378

The problem is that your regex

http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

expects the URL to start with http:// or https://. Also, the dash inside the character class is misplaced.

Edit: Now that you've posted your rules, I suggest this:

^\s*((?:https?://)?(?:[\w-]+\.)+[\w-]+)(/[\w ./?%&=-]*)?\s*$

After a successful match, group 1 will contain the domain, and group 2 will contain the file path, if present.

Upvotes: 3

Pankaj
Pankaj

Reputation: 10105

^(?i)(http|ftp|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$

Upvotes: 0

Related Questions