简体   繁体   English

JavaScript表单验证多个

[英]Javascript form validation multiple

I must make form validation in javascript for 6 input types. 我必须使用6种输入类型的javascript进行表单验证。 When user press on submit button it should give him alert window about the fields which were not typed correctly and turn them into red.. 当用户按下“提交”按钮时,应该为他提供有关未正确键入的字段的警报窗口,并将其变为红色。

<script type="text/javascript">
    function preveriFormo() {

        var ime = document.getElementById('ime');
        var priimek = document.getElementById('priimek');


        if (preveriCrke(ime, "Prosim uporabite samo crke za ime")) {
            if (preveriCrke(priimek, "Prosim uporabite samo crke za priimek")) {
                return true;
            }
        }
        return false;
    }

    function preveriCrke(element, sporocilo) {
        var crke = /^[a-zA-Z]+$/;
        if (element.value.match(crke)) {
            return true;
        } else {
            element.style.backgroundColor = "red";
            alert(sporocilo);
            return false;
        }
    }

</script>

<form onsubmit="return preveriFormo()" >
    Ime <input type='text' id='ime' /><br />
    Priimek <input type='text' id='priimek' /><br />
    <input type="submit" value="Potrdi" />
</form>

My problem is that I dont know how to make alert window with list of not correctly writen fileds and change color( for all of them).. in my example forms are only checked one-by-one. 我的问题是我不知道如何使用未正确写入的文件列表创建警报窗口并更改颜色(对于所有文件)..在我的示例表单中,仅逐一检查。 I would really appriciate if anyone could him me a hint how to solve this 如果有人能给我一个暗示如何解决这个问题,我真的很高兴

take a flag variable.By default put the value to false .If any one of the validation fails then put true . 接受一个flag变量,默认情况下将值设置为false 。如果任何一个验证失败,则设置true Than return flag 比返回标志

return flag;

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

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