Sunday, June 01, 2008

Regular expression for URL validation

/^(?:https?|s?ftp|telnet|ssh|scp):\/\/(?:(?:[\w]+:)?\w+@)?(?:(?:(?:[\w-]+\.)*\w[\w-]{0,66}\.(?:[a-z]{2,6})(?:\.[a-z]{2})?)|(?:(?:25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})))(?:\:\d{1,5})?(?:\/(~[\w-_.])?)?(?:(?:\/[\w-_.]*)*)?\??(?:(?:[\w-_.]+\=[\w-_.]+&?)*)?$/i;

This will do the job.

Labels: , , ,

2 Comments:

Anonymous Anonymous said...

Does not work.
An address like "http://www.test.com?param" is very common but will not pass!

5:54 AM  
Anonymous Lokamaya said...

Not valid if there is "#" character in URL.

@zaenal

8:14 AM  

Post a Comment

<< Home