简体   繁体   English

JavaScript验证不起作用(onSubmit)

[英]Javascript Validation not working (onSubmit)

Vaidation function called on submit. 提交时调用的验证功能。

HTML 的HTML

 <input type="submit" class="submit"  value="submit">

JS JS

window.load = function() {
var form = document.getElementById('form');
  form.onsubmit = function(e) {
      return validate(); // will be false if the form is invalid
  }  
}

Validate() 验证()

function validate() {

    var x = document.forms["form"]["fname"].value;
    var y = document.forms["form"]["pname"].value;
    var email = document.forms["form"]["email"].value;
    var phone = document.forms["form"]["phone"].value;
    var date = document.forms["form"]["date"].value;
    var month = document.forms["form"]["month"].value;
    var year = document.forms["form"]["year"].value;
    return false;
    alert('wass');



    if (x==null || x == "" || isNaN(x) == false) {
        alert("Check Name, It can't have numbers. You can use Roman numbers.");
        return false;}
    else if (y == null || y == "") {
        alert("Picture Name must be filled out");
        return false;       
    }
    else if(email == '' || email.indexOf('@') == -1 || email.indexOf('.') == -1) 
    {
        alert("Insert valid Email Address");
        return false;   
        }
    else if(phone == ''|| phone <1000000000 || phone >9999999999){

        alert("Enter valid phone number");
        return false;   

        }else if(date =='' || date<01 || date >31){

        alert("Enter valid Date ");
        return false;   

        }else if(month =='' || month<1 || month >12){

        alert("Enter valid Month ");
        return false;   

        }else if(year =='' || year<1800 || year >2016){
        alert("Enter valid Year ");
        return false;   

        }



//Function used to make colors red instead of individual codelines
function makeRed(inputDiv){
inputDiv.style.backgroundColor="#AA0000";
//inputDiv.parentNode.style.backgroundColor="#AA0000";
//inputDiv.parentNode.style.color="#FFFFFF";
}

//Function made to clean the divs when the validation is met.
function makeClean(inputDiv){
inputDiv.style.backgroundColor="#FFFFFF";
inputDiv.parentNode.style.backgroundColor="#FFFFFF";
inputDiv.parentNode.style.color="#000000";  
}



}

Form still gets submitted. 表单仍被提交。 Possible issues? 可能的问题?

You'll need to prevent the default form submission using: 您需要使用以下方法阻止默认表单提交:

e.preventDefault();

Place this above your validate function. 将其放在您的验证功能上方。

Then use the submit() function on the form to actually submit the form provided your validation passes. 然后,在您的验证通过的情况下,使用表单上的commit()函数实际提交表单。

At the minute your form is submitting regardless. 无论何时,您的表单都将提交。

You need to prevent default functionality of form submit, by calling e.preventDefault() . 您需要通过调用e.preventDefault()来阻止表单提交的默认功能。 In your case: 在您的情况下:

window.load = function () {
    document.getElementById('form').onsubmit = function (e) {
        if (!validate()) {
            e.preventDefault();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM