Domain Name Validation Script

This javascript function will help you to validate the domain name given in a form field.


  • You can validate the domain name using this javascript function.
  • Domain Name validation takes into account of the following criteria.      
          - domain names should have only alpha numeric values and '-',
          - domain names should not begin/end with '-',
          - domain names should not have special characters,
          - domain extension should be a defined extension.
  • Simple and ease of use.
  • Just copy the code in to your page and use it.


Give Domain Name to validate


<!-- Script by -->
<script type="text/javascript">
function checkDomain(nname)
var arr = new Array(
var mai = nname;
var val = true;
var dot = mai.lastIndexOf(".");
var dname = mai.substring(0,dot);
var ext = mai.substring(dot,mai.length);
if(dot>2 && dot<57)
{for(var i=0; i<arr.length; i++){
if(ext == arr[i])
val = true;break;
val = false;
}}if(val == false){ alert("Your domain extension "+ext+" is not correct"); return false;}else{for(var j=0; j<dname.length; j++){
var dh = dname.charAt(j);
var hh = dh.charCodeAt(0);
if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
{ if((j==0 || j==dname.length-1) && hh == 45)
alert("Domain name should not begin are end with '-'");
return false;
{ alert("Your domain name should not have special characters"); return false;
alert("Your Domain name is too short/long");
return false;
}return true;
<!-- Script by -->
  • Release Date - 15-12-2004
  • Get free version without ©copyright link for just $10/-
  • For customization of this script or any script development, mail to


  • Here we have explained domain name validation using javascript with the help of a form
  • Creating the form
  •  <form name="test" onSubmit="return checkDomain(test.mailf.value)">
    <input name=mailf type=text>
    <input type=submit value=test>
  • Copy the javascript code into the

    <head> tag and html code into the <body> tag.
  • Things will work. The important portion is the onSubmit call in the form. "onSubmit = return checkDomain(test.mailf.value)". We call the method checkDomain with the form fields value as the variable.
  • If the domain check is true, form action will take place, else an alert is displayed.


Related Scripts

Free Javascript Scripts

Ask Questions

Ask Question