Javascript script – code to check / validate website URL in a text field entered by a user

The below is a very simple regular expression in javascript to check/ validate the format of website URL entered by a user.

The same can also be done through JQuery. I will be posting JQuery validation in my next post.

var v = new RegExp();

//Build regular expression
v.compile("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");

//myFormName is the name/id of the form tag i.e. <form name="myFormName" id="myFormName">
if (!v.test(document.myFormName["txtWebsiteUrl"].value))
{
alert("You must supply a valid website Url.");
document.myFormName.txtWebsiteUrl.focus();
return false;
}
Bookmark and Share
Both comments and pings are currently closed.

One Response to “Javascript script – code to check / validate website URL in a text field entered by a user”

  1. May Sprinzl says:

    It is a really good read for me, should admit that you are among the best bloggers I ever saw. Thanks for posting this enlightening post UncleCode.Com – Technology Blog | Javascript script – code to check / validate website URL in a text field entered by a user . Carry on the good work, I have added you to my blogroll.

n