0

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

1
  • @Tim Pietzcker - It should ignore any extra space before and after the url if there is any. Commented May 12, 2011 at 14:57

4 Answers 4

3

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.

3
  • I tried it by adding http or https but still fails. URL may contain any number of dots and this may be a url to file on server (as in question) or url of the default page like msn.com Commented May 12, 2011 at 12:35
  • 1
    the 2nd one fails to validate if i only provide msn.com in text box. Commented May 12, 2011 at 12:38
  • @Umar: Well, what are your rules for validating a URL? You can use (http(s)?://)?([\w-]+\.)+[\w-]+(/[\w ./?%&=-]*)? but that will probably allow stuff you don't want to allow. Commented May 12, 2011 at 13:20
0
^(?i)(http|ftp|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
5
  • @Pankaj: thanks - First one is working but its CASE SENSITIVE. Also invalidate the url if there is any single space at the end. Commented May 12, 2011 at 13:21
  • whole url should be case insensitive. HTTP://www.msn.com will be invalid against the given expression. It should also trim the extra space if there is any. Commented May 12, 2011 at 13:52
  • 1
    These expressions aren't what you need; they incorrectly limit top-level domains to 2 or 3 characters (what about .mobi or .info?), and don't seem to even try to allow for file names at all. Commented May 12, 2011 at 14:04
  • I never save a website shows the url starts with HTTP://www.msn.com instead it should be like msn.com for that us can use ^(http|ftp|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$ Commented May 13, 2011 at 5:04
  • @Umar - I wouldn't try to use Pankaj's answer; he does not appear to have read your question. His expression will not allow file names to be included in a URL. It doesn't even try to. Commented May 13, 2011 at 14:41
0
"(http(s)?://)?([\www]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?" 
0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.