简体   繁体   English

表单验证消息(动态)

[英]form validation message (dynamic)

Is there a way to dynamically tell which .input has yet to be entered? 有没有一种方法可以动态判断尚未输入哪个.input In the code below you can see that if I enter out of order, the #message only counts how many inputs have been populated and displays the message listed in order under numValid == 1, 2, 3, etc . 在下面的代码中,您可以看到,如果我输入的顺序不正确,则#message仅计算已填充的输入数量, #message顺序显示在numValid == 1, 2, 3, etc下的消息。

Can the code be changed to dynamically display a message for the .inputs that have not been populated? 可以更改代码以动态显示尚未填充的.inputs消息吗?

*****Example: if I type in the Last Name and Student ID field, the message will either tell me to enter in the First Name or City field, etc. until they are all populated and the last validation (success message) is displayed***** *****示例:如果我在“ Last NameStudent ID字段中输入信息,则该消息将告诉我在“ First Name或“ City字段中输入信息,等等。被陈列*****

 $("#form input").keyup(function() { var numValid = 0; $("#form input[required]").each(function() { if (this.validity.valid) { numValid++; } }); var progress = $("#progress"), progressMessage = $("#message"); if (numValid == 1) { progress.attr("value", "25"); progressMessage.text("Please Enter the First Name."); } if (numValid == 2) { progress.attr("value", "50"); progressMessage.text("Please Enter the Last Name."); } if (numValid == 3) { progress.attr("value", "75"); progressMessage.text("Please Enter a City."); } if (numValid == 4) { progress.attr("value", "100"); progressMessage.text("You're done, post!"); } }); 
 #mainformdiv { margin-left: auto; margin-right: auto; width: 500px; border: 1px solid; border-radius: 10px; } #form { padding: 20px; } #progress { width: 460px; height: 25px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="mainformdiv"> <form id="form"> <div id="progressdiv"> <progress max="100" value="0" id="progress"></progress> <div id="message">Progress Message...</div> </div> <div class="input"> <label for="userid">Student ID</label><br> <input id="userid" required="required" type="text"> </div> <div class="input"> <label for="firstname">First Name</label><br> <input id="firstname" required="required" type="text"> </div> <div class="input"> <label for="lastname">Last Name</label><br> <input id="lastname" required="required" type="text"> </div> <div class="input"> <label for="city">City</label><br> <input id="city" required="required" type="text"></br> </div> </form> </div> 

Easy to accomplish, just iterate over all of the required fields, and join their ids into a string. 易于实现,只需遍历所有必填字段,然后将其ID连接到字符串即可。 If you want to display a nicer looking name, then just map the IDs to an object first. 如果要显示美观的名称,则只需先将ID映射到对象即可。

 $("#form input").keyup(function() { var numValid = 0; $("#form input[required]").each(function() { if (this.validity.valid) { numValid++; } }); var progress = $("#progress"), progressMessage = $("#message"); const invalidInputs = Array.from(document.querySelectorAll('#form input[required]')) .filter(input => !input.validity.valid) .map(input => input.id); progress.attr("value", numValid * 25); if (numValid == 4) { progressMessage.text("You're done, post!"); } else { progressMessage.text('Please fill out the following fields: ' + invalidInputs.join(', ')); } }); 
 #mainformdiv { margin-left: auto; margin-right: auto; width: 500px; border: 1px solid; border-radius: 10px; } #form { padding: 20px; } #progress { width: 460px; height: 25px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="mainformdiv"> <form id="form"> <div id="progressdiv"> <progress max="100" value="0" id="progress"></progress> <div id="message">Progress Message...</div> </div> <div class="input"> <label for="userid">Student ID</label><br> <input id="userid" required="required" type="text"> </div> <div class="input"> <label for="firstname">First Name</label><br> <input id="firstname" required="required" type="text"> </div> <div class="input"> <label for="lastname">Last Name</label><br> <input id="lastname" required="required" type="text"> </div> <div class="input"> <label for="city">City</label><br> <input id="city" required="required" type="text"></br> </div> </form> </div> 

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

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