谁能告诉我如何改进此代码,最重要的是对我的电子邮件验证进行排序以在 Z 不等于任何内容或字符串“电子邮件”的所有情况下工作
所有字段均以已输入的适当措辞开头,作为客户示例。
提前致以亲切的问候。
function validateForm()
{
//Uses HTML field IDs
var x=document.forms["myForm"]["name"].value;
var y=document.forms["myForm"]["phone"].value;
var z=document.forms["myForm"]["email"].value;
//Name locator
if (x==null || x=="" || x=="Name")
{
alert("Please enter the your name.");
return false;
}
//Contact method locator
if ((y==null || y=="" || y=="Phone Number")&&(z==null || z=="" || z=="Email"))
{
alert("Please enter a contact method.");
return false;
}
//Phone numeric validation, this runs if Email field is not edited
if (z==null || z=="" || z=="Email")
{
if (isNaN(y)||x.indexOf(" ")!=-1)
{
alert("Telephone must be a numeric value.");
return false;
}
}
//Phone length validation, this runs if Email field is not edited
if (z==null || z=="" || z=="Email")
{
if (y.length > 14)
{
alert("Telephone must be valid.");
return false;
}
}
//Email validation, does not work, this should run only when something is entered into the field
if (z!=null || z!="" || z!="Email")
{
var atpos=z.indexOf("@");
var dotpos=z.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=z.length)
{
alert("This is not a valid e-mail address");
return false;
}
}
}