Phone number validation using JavaScript

Validating HTML Forms using JavaScript:

JavaScript can be used for validating HTML forms.

If the field of form is empty, the function named ‘validateHTML’ sends an alert message and will return ‘false’ for preventing the submission of this form. 

Syntax of validate function in JavaScript for HTML:

 function validateHTML()

{    

var y = document.forms["sampleForm"]["firstname"].value;

if (y == null || y == "")

 {

alert("first name should not be empty");    

return false;

} }

JavaScript program for validating phone numbers: 

function ValidateForm() 

{     

var a = document.formphone.textPhone.value;  

var b = document.formmobile.textMobile.value;       

if(isNaN(a)||a.indexOf(" ")!=-1)          

{           

 alert("Enter value in numbers");

 return false;       

}         

 if (a.length>8)        

 {    

  alert("enter lesser than or equal to 8 characters");  

  return false; 

  }          

if (x.charAt(0)!="4")  

{             

alert("it must begin with 4");

return false          

}           

if(isNaN(b)||b.indexOf(" ")!=-1)          

{

alert("Enter only numeric values");

return false;          

}         

if (b.length>10)

{            

alert("enter only ten characters"); 

return false; 

}

if (b.charAt(0)!="8")          

{              

alert("it must begin with 8 ");              

return false          

}   }