简体   繁体   English

动态验证Javascript中的值列表

[英]Dynamic validation of list of values in Javascript

I have a form with dynamically generated input fields.In these input fields , some items are mandatory and needs to be filled in by the user.While submitting the form , i need to validate the form and check whether the mandatory fields are not empty.How can i achieve this using javascript? 我有一个带有动态生成的输入字段的表单。在这些输入字段中,有些项目是必填项,需要由用户填写。在提交表单时,我需要验证表单并检查必填项是否不为空。如何使用javascript实现此目的? Any suggestions are welcome. 欢迎任何建议。

Thanks for your valuable time and help 感谢您的宝贵时间和帮助

If you are using HTML 5, you can using the required attribute in the input tag else you can use something like below to verify each field before submit. 如果您使用的是HTML 5,则可以在input标签中使用required属性,否则可以使用以下类似的方法在提交之前验证每个字段。

 $(document).ready(function(){ $("#submit").click(function(){ var failed = false; $(".required").each(function(){ if ($(this).val() == "") { failed = true; } }); if (failed) { alert("Mandatory field is empty!"); } else { alert("Proceed"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form method="post" id="myFomr"> <input name="text1" type="text" class="required" value=""><br> <input name="text2" type="text" value=""><br> <input name="text3" type="text" class="required" value=""><br> <form> <button id="submit" type="button">Submit</button> 

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

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