简体   繁体   English

JavaScript验证不适用于g:form验证

[英]Javascript validation does not work for g:form validation

I am attempting to validate all form elements using a javascript function which is as follows: 我正在尝试使用如下的javascript函数来验证所有表单元素:

function validateForm() {
                    //var inpObj = document.getElementById("myForm").elements;
                    var inpObj = document.getElementById("myForm");
                    for (var i = 0; i < inpObj.length; i++) {
                        if (inpObj.checkValidity() == false) {
                            document.getElementById("validationErrors").innerHTML = inpObj.validationMessage;
                        } else {
                            document.getElementById("validationErrors").innerHTML = "Input Ok";
                        }
                    }
                }

This should validate all elements within myForm and display their error messages if they do not pass validation. 这将验证myForm中的所有元素,如果未通过验证,则显示其错误消息。 However it is not displaying any errors in the validation.even though elements are being left blank on purpose. 但是,即使元素被故意留为空白,它在验证中也不显示任何错误。

The form tag is as follows: <g:form name="myForm" action="confirmApplyFormSaveToGroup" novalidate="novalidate"> with the action being the redirect if the validation passes. 表单标签如下: <g:form name="myForm" action="confirmApplyFormSaveToGroup" novalidate="novalidate"> ,如果通过验证,则该操作为重定向。

The function is called when the form is submitted as follows: 提交表单时,将调用以下函数:

<div class="ui-block-b"><button type="submit" data-theme="d" onclick="validateForm()">Submit</button></div>

The errors are supposed to display in the following paragraph: 错误应该在以下段落中显示:

<p id="validationErrors"></p>
var inpObj = document.getElementById("myForm");
<g:form name="myForm" action="confirmApplyFormSaveToGroup" novalidate="novalidate">

You are using getElementById but dont have ID on your form. 您正在使用getElementById但表单上没有ID。

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

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