Solved

How to validate a url

Posted on 2011-09-21
10
227 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

744 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

13 Experts available now in Live!

Get 1:1 Help Now