Validating url using javascript

Some times we might need to check if the url entered by the user in the text box is a valid one or not. Here is a sample code to check that.

function checkUrl()
var theUrl=document.getElementById(‘url’).value;

if(theUrl.match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) ||

alert(“valid Url”);
alert(“Not valid Url”);
return false;
<form name=”frmurlvalidation”>
Your URL:<input value=”http://” name=”url” size=”50″>
<input type=”button” value=”Validate”>

Posted in Javascript Tagged with: ,