简体   繁体   English

如何验证页面上存在的所有输入?

[英]How to validate all inputs which exists on page?

All inputs from page页面的所有输入

I have this html page which is dynamical created, which contains some divs.我有这个动态创建的 html 页面,其中包含一些 div。 Every div-question(0,1,2, etc) contain an input based on which answer type the user chose.每个 div-question(0,1,2, etc) 都包含一个基于用户选择的答案类型的输入。 I want to validate every single inputs from page and:我想验证页面中的每一个输入,并且:

If value of one input type number,text,date is != "" alert("something")如果一个输入类型 number,text,date 的值为 != "" alert("something")
else send the value in an array;否则将值发送到数组中;
If checkbox/radio is not checked alert("something");如果未选中复选框/收音机警报(“某事”);

I tried something like this:我试过这样的事情:

        let nrDiv = document.getElementsByClassName("div-question");
        let existInput = nrDiv[0].querySelector("input[type='text']");
        let numberInput = nrDiv[0].querySelector("input[type='number']");
        if (document.body.contains(existInput)) {
            for (let i=0; i < nrDiv.length ;i++) {
                let container = document.getElementsByClassName("div-questions" + i + "");
                let userInputAnswer = container[0].querySelector("input[type='text']");
                if (userInputAnswer.value == "") {
                    alert("Adaugati un raspuns!")
                    return;
                } 
                 if (userInputAnswer.value != ""){
                    let answer = {
                        question: questions[i].textQuestion,
                        answer: userInputAnswer.value
                    }
                    answers.push(answer);
                }
            }
        }

It's working but if I come with another for loop, for input type="number" is not working anymore.它正在工作,但如果我使用另一个 for 循环,for input type="number" 将不再工作。 I'm getting value null.我得到了价值 null。 So if I come with this:所以,如果我带着这个:

       if (document.body.contains(numberInput)) {
            for (let i=0; i < nrDiv.length ;i++) {
                let container = document.getElementsByClassName("div-questions" + i + "");
                let userInputAnswer = container.querySelector("input[type='number']");
                if (userInputAnswer.value == "") {
                    alert("Adaugati un raspuns!")
                    return;
                } 
                 if (userInputAnswer.value != ""){
                    let answer = {
                        question: questions[i].textQuestion,
                        answer: userInputAnswer.value
                    }
                    answers.push(answer);
                }
            }
        }

And for the checkbox and radio inputs I don't have any idea.对于复选框和无线电输入,我不知道。 I want something like this:我想要这样的东西:

If all inputs are not empty and minimum one checkbox/radio is checked, send the answer and question in an array else alert("error");如果所有输入都不为空并且至少选中一个复选框/单选框,则将答案和问题发送到数组中 else alert("error");

I feel like this is simple once you add the required attribute and/or a pattern .一旦添加了required的属性和/或pattern ,我觉得这很简单。

This is a simple POC:这是一个简单的 POC:

 <form action=""> <input type="text" required> <input type="number" required> <input type="checkbox" required> <input type="radio" required> <input type="date" required> <button type="submit">Submit</button> </form>

Notice that when you click the submit button, it does the verification you want, in this case != "" .请注意,当您单击submit按钮时,它会执行您想要的验证,在本例中!= ""

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

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