2015-09-07 88 views
0

我想我得到了驗證,並且警報消息正確顯示了名稱和值。但是我的問題是:即使驗證不正確,警報框也會持續顯示信息。如果驗證全部爲真,我希望系統只顯示輸入的信息。在文本字段中輸入驗證和顯示信息

這裏是我的HTMTL:

<font face = "Algerian" ><h2>We'd Like to Know More About You!</h2></font> 
<form name="info"> 
Name: &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <input type = "text" id = "inf" name = "name" placeholder="Enter a valid name">*</br></br> 
Address: &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <input type = "text" id = "inf" name = "address" placeholder="Enter a valid address"></br></br> 
Email Address: &nbsp <input type="email" id = "inf" name="email" placeholder="Enter a valid email address">*</br></br> 
Phone Number: &nbsp <input type = "tel" id = "inf" name ="PN" placeholder="Enter a valid phone number">*</br></br> 
<hr/> 
</form> 


<input type = "button" onClick = "displayInfo()" value ="Submit Info"> 
</form> 

這裏是我的javascript:

<script type = "text/javascript"> 
///////////validations 
function ValEntry(){ 
    var valid = true; 

    if (document.info.name.value== ""||document.info.address.value== ""||document.info.email.value== ""||document.info.PN.value == "") 
    { 
     alert ("Please fill up the remaining text boxes"); 
     valid = false; 
    } 
    return valid; 
} 

function checkemail(){ 
    var testresults1 
    var str=document.info.email.value 
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i 

    if (filter.test(str)) 
    testresults1=true 
    else{ 
    alert("Please input a valid email address!") 
    testresults1=false 
    } 
return (testresults1) 
} 

function checkphone(){ 
    var testresults2 
    var int = document.info.PN.value 
    var filter2= /[1-9]/g; 

    if (filter2.test(int)) 
    testresults2=true 
    else{ 
    alert("Please input a valid phone number!") 
    testresults2=false 
} 
return (testresults2) 
} 

function validate(){ 
    if (document.info.email.value) 
    return checkemail() 
    else{ 
    return true 
    }  
} 

function validate2(){ 
    if (document.info.PN.value) 
    return checkphone() 
    else{ 
    return true 
    }  
} 



function FinalVal(){ 
ValEntry(); 
validate(); 
validate2(); 

} 



function displayInfo(){ 
FinalVal(); 

    var fields=["name", "address", "email", "PN"]; 
    var fieldNames=["Name", "Address", "Email Address", "Phone Number"]; 
    var msg = ""; 

    for(i=0;i<fields.length;i++) 
    msg += fieldNames[i] + ": " + document.info[ fields[i] ].value + "\n"; 

    alert(msg); 
    return true; 

} 


</script> 

回答

1

你沒有做你的驗證返回值什麼。嘗試這樣的:

function FinalVal(){ 
    return ValEntry() && validate() && validate2(); 
} 

function displayInfo(){ 
    if (FinalVal()) { 
     var fields=["name", "address", "email", "PN"]; 
     var fieldNames=["Name", "Address", "Email Address", "Phone Number"]; 
     var msg = ""; 

     for(i=0;i<fields.length;i++) 
      msg += fieldNames[i] + ": " + document.info[ fields[i] ].value + "\n"; 

     alert(msg); 
    } 
    return true; 
} 
+0

再次謝謝你! (y)的 – Raios