简体   繁体   English

javascript复选框验证检查

[英]javascript checkbox validation checking

I have problem with my checkbox validation on my Tip Area Soccer Game. 我的Tip Area Soccer Game的复选框验证存在问题。 So if the user likes to tip a game, he must have to use 2 inputs field and the confirmation checkbock. 因此,如果用户喜欢给小费游戏,则必须使用2个输入字段和确认检查框。 But if the user uses 2 inputs fields and "more than one" confirmation checkbox, then he must get an alert error message. 但是,如果用户使用2个输入字段和“多个”确认复选框,则他必须收到警报错误消息。 Because the right combination consists from "2 input fields + confimration checkbox" Here, in my screenshot you see the right combination for the green submit button: 因为正确的组合是由“ 2个输入字段+确认复选框”组成的 ,所以在我的屏幕快照中,您可以看到绿色的“提交”按钮的正确组合:

在此处输入图片说明

And in the second screenshot you see the error combination: 在第二张屏幕截图中,您会看到错误组合:

在此处输入图片说明

I don't have any idea of how to code the alert for the error message on the display if the user uses the second combination. 如果用户使用第二种组合,我不知道如何在显示屏上为错误消息编码警报。

Here's my Javascript code: 这是我的Javascript代码:

   function chkAddTip(){

        var inputs = document.getElementById('AddTip').getElementsByTagName('input');

        // boolean flag
        var foundValid = false;

        // early exit the loop if at least one valid bet has been found
        for (var i = 0; i < inputs.length && !foundValid; i += 3){
            if (inputs[i].type !== "submit" && (inputs[i].value && inputs[i + 1].value && inputs[i + 2].checked)) {
                // set the flag to true when a valid bet is found
                foundValid = true;
            }
        }

        // determine the return value depending on the flag
        if (foundValid) {
            return true;
        }
        else {
            alert("Bitte deinen Tipp und die Bestättigung abgeben.")
            inputs[0].focus();
            return false;
        }

And here my form code: 这是我的表单代码:

<form action="Ctipservlet" id="AddTip" onsubmit="return chkAddTip()" method="POST">
    <div id="inhalt"><h1>Tip Area</h1>
    <table>
        <tbody>
            <tr>
               <th>Playdate</th> 
               <th>Playtime</th> 
               <th>Games</th>
               <th>Your Tip</th>
               <th>Confirmation</th>
            </tr>
            <tr>
               <td>21.07.</td>
               <td>19:30 Uhr</td>
               <td>Schalke - Bayern</td>
               <td><input style="width:30px!important; text-align: center;" type="text" name="team_a0" maxlength="2" size="2">:<input style="width:30px!important; text-align: center;" type="text" name="team_b0" maxlength="2" size="2"></td>
               <td><input type="checkbox" name="check0"></td>
            </tr>
            <tr>
               <td>22.07.</td>
               <td>20:30 Uhr</td>
               <td>Dortmund - Leverkusen</td>
               <td><input style="width:30px!important; text-align: center;" type="text" name="team_a1" maxlength="2" size="2">:<input style="width:30px!important; text-align: center;" type="text" name="team_b1" maxlength="2" size="2"></td>
               <td><input type="checkbox" name="check1"></td>
           </tr>
           <tr>
               <td>23.07.</td>
               <td>15:30 Uhr</td>
               <td>Wolfsburg - Nürnberg</td>
               <td><input style="width:30px!important; text-align: center;" type="text" name="team_a2" maxlength="2" size="2">:<input style="width:30px!important; text-align: center;" type="text" name="team_b2" maxlength="2" size="2"></td>
               <td><input type="checkbox" name="check2"></td>
           </tr>
       </tbody>
    </table>
    <input class="button_green" type="submit" name="tip" value="Submit Tip">
    <input class="button_blue" onclick="this.form.onsubmit = null" type="submit" name="back" value="Back">
    </div>
</form>

I hope someone have idea for this checking 我希望有人对此检查有想法

We talked in chat, looked at this. 我们在聊天中交谈,看着这个。 Below is my solution but first... this is badly structured. 下面是我的解决方案,但首先...结构不正确。 You are doing validation and form submission in one step. 您只需一步即可完成验证和表单提交。 I would break it up in to more then one. 我将其分解为多个。 It just makes debugging/extension easier in the long run. 从长远来看,它只会使调试/扩展变得更容易。 I would validate inputs first. 我将首先验证输入。 Save into object. 保存为对象。 Send to submit function. 发送提交功能。 Then submit to DB. 然后提交给数据库。 This right now is trying to do all that in one function. 现在,此功能正在尝试在一个功能中完成所有操作。 Anyway.... 无论如何....

The problem is the loop for checking if there are inputs. 问题是检查是否有输入的循环。 When it finds the first true result it will call a submit and exit the function. 当找到第一个真实结果时,它将调用一个提交并退出该函数。

That's why you can input 2 fields, a checkbox and other checkboxes and foundValid is true. 这就是为什么您可以输入2个字段,一个复选框和其他复选框,并且foundValid为true的原因。

My fix was to check for invalid input first. 我的解决方法是先检查输入是否无效。 Have an outer boolean as an error. 将外部布尔值作为错误。 If it passes it submits. 如果通过,则提交。 The only issue with it right now is empty fields will return a true condition and submit. 现在唯一的问题是空白字段将返回真实条件并提交。 Check the JS Fiddle http://jsfiddle.net/Komsomol/EDdUU/4/ 检查JS小提琴http://jsfiddle.net/Komsomol/EDdUU/4/

var error = false;
var results = [];

function chkAddTip() {
    var inputs = document.getElementById('AddTip').getElementsByTagName('input');
    console.log(inputs);

    // early exit the loop if at least one valid bet has been found
    for (var i = 0; i < inputs.length; i += 3) {

        if (!inputs[i].value && !inputs[i + 1].value && inputs[i + 2].checked) {
            console.log("inputs inputed wrong!");
            error = true;
        } else {
           results.push(inputs[i].value,inputs[i + 1].value,inputs[i + 2].checked);
        }
    }    
    submit();
}

function submit(){
    console.log(results, error);
    if(error == true){
        alert("error in inputs");
    } else {
        alert("posting to server");
    }   
}    

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

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