An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Tuesday, April 27, 2010

How to validate Website Address using Regular Expressions?

Regular Expression for Valid Website Address

How to valid the input for a website address in your javascript using regular expression? - A key question many of us are struggling to find an answer.

Is there really a regular expression that will seize invalid website address?
Fortunately, the answer is yes.

Regex_Pattern = "/^((https?|ftp|news):\/\/)?www\.([a-z]([a-z0-9\-]*)+\.(aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|lan|mil|mobi|museum|nato|name|net|org|pro|store|tel|travel|web|[a-z]{2}|[a-z]{2}\.[a-z]{2}))$/"

The above regular expression shall be used in your regex functions in any programming language to seize the invalid website address.

Example (using Python):

result = re.match(Regex_Pattern, website_string)