Solved

How to validate a url

Posted on 2011-09-21
10
228 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
validating special characters 5 40
regex101 site usage 5 318
.net string parse 18 52
Regular expression to allow alpha numeric plus some other characters. 12 42
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…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now