[RESOLVED]Validate Website URL

Hi Friends,

I want to validate website URL in Javascript.

function fnWebsiteUrl(sCtrl) {
if (document.getElementById(” + sCtrl + ”).value != "")
{
var url = /(http(s)?:\)?([w-]+.)+[w-]+[.com|.in|.org|.net|.edu]+([?%&=]*)?/;
if ((url.test(document.getElementById(” + sCtrl + ”).value))) {
return true;
}
else {
alert("Please enter valid URL");
document.getElementById(” + sCtrl + ”).value= "";
document.getElementById(” + sCtrl + ”).focus();
return false;
}}
}

Pl suggest if this correct or any corrections. Valuable Feedback is appreciated.

Have you tested it? Does it work? If so, it’s correct. 

The only thing that might need ot be lloked at i the Regex pattern, but you need to explain what kinds of url you want to accept. At the moment, you are only accepting .com, .in, .org, .nrt and .edu. There are thousands of other TLDs out there such as .co.uk,
.ca, .ly, .ac and so on. Do you want to reject all of them?

Thanks for response.

I just ask you whether I can validate only URL abc.abc instead of exact domain name like .net,.com.,.in etc why because it will not allow .ca,.ly thousands of domain.

I want to know validation standard of website URL.

Pl advice.

Hi Uma,

please try the code below to validate standard url:

   function fnWebsiteUrl(sCtrl) {
      
            if (document.getElementById(sCtrl).value != "") {
                var url = /(http(s)?:\)?([w-]+.)+[w-]+[.com|.in|.org|.net|.edu]+([?%&=]*)?/;
                if ((url.test(document.getElementById(sCtrl).value))) {
        
                    return true;
                }
                else {
                    alert("Please enter valid URL");
                    document.getElementById('' + sCtrl + '').value = "";
                    document.getElementById('' + sCtrl + '').focus();
                    return false;
                }
            }
        }

Best Regards,

Kevin Shen.

Leave a Reply