表单验证无法正常工作

时间:2012-11-30 12:04:55

标签: java javascript html validation

我正在尝试制作验证表单。我无法让我的验证表格正常工作,手机功能无法正常工作,我不确定即使用户按提交即可提交,有人可以帮我吗?

HTML:

<form onsubmit="return validate();" name="formValidation">
    <label>First Name:</label>
    <input type="text" name="firstName" /><br /><br />
    <label>Last Name:</label>
    <input type="text" name="lastName" /><br /><br />
    <label>E_mail:</label>
    <input type="text" name="Email" onchange="validateEmail(this.value)"/><br /><br />
    <label>Confirm E_mail:</label>
    <input type="text" name="confirmEmail" onchange="validateEmail(this.value)"/><br /><br />
    <label>Address:</label>
    <input type="text" name="Address" /><br /><br />
    <label>Telephone nr:</label>
    <input type="text" name="fld" /><br /><br />
    <br />
    <p>submit your form: </p><input type="submit" value="Submit" />
</form>

JS:

function validate(){
    if(document.formValidation.firstName.value == "" ||
    document.formValidation.lastName.value == "" ||
    document.formValidation.Email.value == "" ||
    document.formValidation.confirmEmail.value == "" ||
    document.formValidation.Address.value == "" ||
    document.formValidation.fld.value == "")
    {
        alert("Please fill all the boxes before submitting!");
        return false;
    } else if (telPhone(document.formValidation.fld.value)!=""){
        alert(error)
        return false
    }else {

        alert('Your form has been submitted!');
    }

}
function validateEmail(Email)   
{  
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Email))  
  {  
    return true;  
  }  
    alert("You have entered an invalid email address!")  
    return (false) 
}

function telPhone(fld) {
    var error = "";
   if (fld == "") {
        error = "You didn't enter a phone number.";
    } else if (isNaN(fld)) {
        error = "The phone number contains illegal characters.";
    } else if (fld.length != 10) {
        error = "The phone number is the wrong length. Make sure you included an area code."
    }

    return error;
}

1 个答案:

答案 0 :(得分:0)

<form onsubmit="return validate();" name="formValidation"  action="fileto redirect here" method="post">