Solved

How to validate a url

Posted on 2011-09-21
10
231 Views
Last Modified: 2012-05-12
0
Comment
Question by:dvcphp
  • 7
  • 3
10 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577143
This one:
/^(https?:\/\/)?(\w[\w\-\.]*)+(\/(\w[\w\-\.]*)+)+(\?[\w\=\%\&]*)?(\#[\w]*)?/

Open in new window

will not allow the // after .com as in yout third line example.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577201
Sorry, ignore the upper.

Check please this:
/^(https?:\/\/)?(\w[\w\-\.]*){3,}(\.(\w[\w\-\.]*){2,}){2,}(\/(\w[\w\-\.]*)+)?(\?[\w\=\%\&]*)?(\#[\w]*)?/

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577211
Sorry I missed the $ at the end.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 63

Expert Comment

by:Zvonko
ID: 36577220
If you are extracting URLs the use \b patter instead of string begin and end characters ^and $
0
 

Author Comment

by:dvcphp
ID: 36577249
Hi Zvonko,
I tried the code.It seems to work only with the third condition.I am sorry I mistyped 2 // .It should be one /
Is it possible to have all the 3 conditions working.

Also I need to check the same expression with something below

www.something.org/body.sgs?id=3435123

www.something.org/#!comm.home    and

www.something.org/About+Us/Co+Integrity/Comm+Needs+help
Thanks,
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577443
Check this please:
/^(https?:\/\/)?(\w[\w\-\.]*){3,}(\.(\w[\w\-\.]*){2,}){2,}(\/(\w+([\w\-\.\+]+\/?)*)*)*(\?[\w\=\%\&\+\.]*)?(\#[\w\!\.\+]*)?$/

Open in new window

0
 

Author Comment

by:dvcphp
ID: 36577473
Thank you so much.That worked like a charm.

Thank you again.You are truely a Genius.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577488
<|:-)
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 36577518
One additional note: the last expression allows this:
www.something.org#!comm.home 

I wanted to avoid that but my IE8 browser slowed down and after several loops crashed.

This is the expression to force a slash before query or hash url parameters:

/^(https?:\/\/)?(\w[\w\-\.]*){3,}(\.(\w[\w\-\.]*){2,}){2,}(\/(\w+([\w\-\.\+]+\/?)*)*(\?[\w\=\%\&\+\.]*)?(\#[\w\!\.\+]*)?)?$/

Open in new window

0
 

Author Comment

by:dvcphp
ID: 36577543
Thanks for letting me know.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question