Skip to main content

Javascript email and phone validation example

Email validation:

var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var email = document.myform.email.value;

if(reg.test(email) == false) { alert("Invalid email"); return false; }

Phone number validation:

var telnoRegxp = /^([0-9]+)$/;
var phone = document.myform.phone.value;

if (telnoRegxp.test(phone) != true) { alert("Invalid phone no"); return false; }

Comments