누구든지이 코드를 향상시킬 수있는 방법을 알려주고 가장 중요한 것은 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;
}
}
}
당신은'경고 (z는)'는 값이 유효성 검사를하기 전에되고 무엇을보고하기 위해 시도? – DevlshOne
몇 가지 팁 : a) 벡터 데이터 등을 나타내지 않으면 ** 이름 변수'x, y, z '를 ** 절대 사용하지 마십시오. b) 들여 쓰기를 수정하십시오. c) 코드 DRY : 'null', '' ''또는 주어진 값 (예 : '이름', '이메일', '전화 번호')을 확인하는 함수를 만듭니다. – ComFreek
z가 0이거나 z가 빈 문자열이거나 z가 "전자 메일"일 때 조건이 true가됩니다. 이런 식으로 작동하지 않니? 문제가 정확히 무엇입니까? 개선에 대한 조언을 원하면 자리 표시 자 속성을 사용하면 "이메일"을 확인할 필요가 없습니다. http://www.w3schools.com/tags/att_input_placeholder.asp –