Thursday, May 13, 2010

Checking valid URL using javascript Regular Expressions

Using this function you can check is given URL valid or not. If url is valid this function will return true otherwise false


function validateUrl(url)
{
var regexp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/
return regexp.test(url);
}

//Usage
var is_valid = validateUrl('http://google.com');

In this case "is_valid" is true

No comments:

Post a Comment