我认为我的验证是正确的,警报信息正确显示了名称和值。但我的问题是:即使验证不正确,警报框也会继续显示信息。我希望系统只显示输入的信息,如果验证都是真的。
这是我的HTMTL:
<font face = "Algerian" ><h2>We'd Like to Know More About You!</h2></font>
<form name="info">
Name:                 <input type = "text" id = "inf" name = "name" placeholder="Enter a valid name">*</br></br>
Address:             <input type = "text" id = "inf" name = "address" placeholder="Enter a valid address"></br></br>
Email Address:   <input type="email" id = "inf" name="email" placeholder="Enter a valid email address">*</br></br>
Phone Number:   <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>
答案 0 :(得分: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;
}