regex - Javascript Email validation that allows a null value -
i have on submit function checks email field have formatted address. works fine. however, want field allow null value particular input optional , not required. know should simple, im missing obvious. point me in right direction...
edit: providing entire form validation time. every time replace email portion breaking other pieces....
function validatesmsform() { if (smsform.pid_form.value.length < 4) { alert("please enter valid partner id"); return false; } if (smsform.area.value.length < 3) { alert("please enter valid 10-digit cell phone number"); return false; } if (smsform.prefix.value.length < 3) { alert("please enter valid 10-digit cell phone number"); return false; } if (smsform.line.value.length < 4) { alert("please enter valid 10-digit cell phone number"); return false; } <!-- email validation here if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.emailaddress.value)) { return (true) } alert("please enter valid email address") return (false) --> }
like this
assuming
<form onsubmit="return validatesmsform(this)">
javascript:
function isemail(str) { return !str || /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(str); } function validatesmsform(smsform) { if (smsform.pid_form.value.length < 4) { alert("please enter valid partner id"); return false; } if (smsform.area.value.length < 3 || smsform.prefix.value.length < 3 || smsform.line.value.length < 4) { alert("please enter valid 10-digit cell phone number"); return false; } if (!isemail(smsform.emailaddress.value)) { alert("please enter valid email address") return false; // } return true; // allow submission }