Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to validate a url

Posted on 2011-09-21
10
Medium Priority
?
237 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…

916 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