2014-10-06 4 views
1

전자 메일 유효성 검사 프로그램부터 유효성 검사가 중지되는이 문제가 발생합니다.자바 스크립트 유효성 검사 :

나는 며칠 후에도 그것을보고 왜 누군가 내 실수를 지적 할 수 있는지 궁금해하고 있습니다.

자바 스크립트 부분 :

function validateForm() 
{ 
var x=document.forms["myForm"]["firstname"].value; 

if (x==null || x=="") 
    { 
    alert("First name must be filled out"); 
    return false; 
    } 

var x=document.forms["myForm"]["lastname"].value; 

if (x==null || x=="") 
    { 
    alert("Last name must be filled out"); 
    return false; 
    } 

var x=document.forms["myForm"]["age"].value; 

if (x==null || x=="" || x < 18 || x > 110 || isNaN(x)) 
    { 
    alert("Age must be 18-110"); 
    return false; 
    } 

var x=document.forms["myForm"]["email"].value; 
var atpos=x.indexOf("@"); 
var dotpos=x.lastIndexOf("."); 

if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) 
    { 
    alert("Not a valid e-mail address"); 
    return false; 
    } 

var x=document.forms["myForm"]["phone"].value; 
var pattern = /^\(?([0-9]{2})\)?[ ]+([0-9]{4})[ ]+([0-9]{4})$/; 

if (x==null || x=="") 
    { 
    alert("Not a valid phone number"); 
    return false; 
    } else if (x.match(pattern)) { 
    return true; 
    } else { 
    alert ("Phone number must be in the following format: xx xxxx xxxx"); 
    return false; 
    } 
} 

이 양식의 일부입니다

<form id="myForm" action="../includes/create-user.php" onsubmit="return validateForm();" method="post"> 
    <div id = "table"> 
     <table> 
     <tr> 
      <td>First Name: </td> 
      <td><input type="text" name="firstname" id="firstname"/></td> 
     </tr> 
     <tr> 
      <td>Last Name: </td> 
      <td><input type="text" name="lastname" id="lastname"/></td> 
     </tr> 
     <tr> 
      <td>Age: </td> 
      <td><input type="text" name="age" id="age"/></td> 
     </tr> 
     <tr> 
      <td>E-mail (username): </td> 
      <td><input type="text" name="user" id="user1"/></td> 
     </tr> 
     <tr> 
      <td>Password: </td> 
      <td><input type="password" name="pass" id="pass1"/></td> 
     </tr> 
     <tr> 
      <td>Phone: </td> 
      <td><input type="text" name="phone" id="phone"/></td> 
     </tr> 
     </table> 
    </div> 
    <div> 
     <input type="submit" name="submit" id="submit" value="Register"/> 
    </div> 
</form> 
+0

클라이언트 측 유효성 검사는 클라이언트가 유효성 검사 스크립트를 비활성화하고 검증되지 않은 콘텐츠를 웹 사이트에 제출할 수 있음을 의미합니다. 브라우저는 항상 적의 손에 있습니다. 답장을 보내 주셔서 감사합니다. – Mark

+1

나는 방금 자바 스크립트에 대해 배우기 시작한 학생입니다. 나는 그것이 클라이언트 측으로부터의 SQL 주입과 같은 경우를 방지하기 위해 클라이언트 측 검증과 서버 측 검증을 갖는 것이 가장 좋은 방법이라는 것을 이해한다. – Ong

답변

1

눈에 띄는 첫 번째 것은 당신이 당신의 분야 지정하는 것입니다 : ID로

<td><input type="text" name="user" id="user1"/></td> 

을 = 'user1' 그리고 스크립트에서 이메일을 찾으십시오

var x=document.forms["myForm"]["email"].value; 
+1

감사합니다. 나는 그것을 스크립트 부분에서 'user1'로 변경했으며 효과가 있었다. 미안해, 놓치지 말았어야 했어. – Ong

관련 문제