Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在JSF中提交表单之前使用JS进行客户端验证_Javascript_Validation_Jsf - Fatal编程技术网

Javascript 在JSF中提交表单之前使用JS进行客户端验证

Javascript 在JSF中提交表单之前使用JS进行客户端验证,javascript,validation,jsf,Javascript,Validation,Jsf,我想在我的注册表上做一些客户端验证。它应该只是停止提交并显示一些文本,帮助用户理解哪里出了问题 这是我的XHTML: <h:form> <div class="form-group mx-auto"> <h:outputLabel for="emailInput" value="Email"></h:outputLabel>

我想在我的注册表上做一些客户端验证。它应该只是停止提交并显示一些文本,帮助用户理解哪里出了问题

这是我的XHTML:

            <h:form>
                <div class="form-group mx-auto">
                    <h:outputLabel for="emailInput" value="Email"></h:outputLabel>
                    <p:inputText type="text" class="form-control" id="emailInput"   value="#{signupBackingBean.email}" required="true"></p:inputText>
                    <small id="emailError1" class="form-text">Field is required</small>
                    <small id="emailError2" class="form-text">Not avalid emailadress</small>

                </div>
                <div class="form-group mx-auto">
                    <h:outputLabel for="usernameInput" value="Username"></h:outputLabel>
                    <p:inputText type="text" class="form-control" id="usernameInput"  value="#{signupBackingBean.username}" required="true">
                        <f:validateLength minimum="5" maximum="20"></f:validateLength>
                    </p:inputText>
                    <p:message for="@previous" />
                </div>
                <div class="form-group mx-auto">
                    <h:outputLabel for="passwordInput" value="Password"></h:outputLabel>
                    <p:password class="form-control" id="passwordInput"  value="#{signupBackingBean.password}" match="passwordInput2" feedback="true" required="true" inline="true" ></p:password>
                </div>
                <div class="form-group mx-auto">
                    <h:outputLabel for="passwordInput2" value="Password Confirmation"></h:outputLabel>
                    <p:password class="form-control" id="passwordInput2"  value="#{signupBackingBean.password}" required="true"></p:password>
                    <p:message for="@previous" />
                </div>
                <div class="form-group mx-auto">
                    <h:commandButton type="submit" class="btn btn-primary mx-auto btn-block" onclick="return showErrorMessages()"  action="#{signupBackingBean.add}" value="Register" ></h:commandButton>
                </div>
            </h:form>

CSS:

计划是触发Primeface验证(不能为null)并使描述文本内联。然而,我不确定如何做到这一点。现在我只是想把它连接到SubmitCommandButton上,但是更早的时候做起来会更容易,这也会起作用


提前谢谢

问题是一个简单的语法错误。使用JS设置样式属性时,属性(如本例中的内联)必须采用字符串形式。所以我要做的是将我的JS文件更改为:

function showErrorMessages() {
        document.getElementById("emailError1").style.display = "inline";
}

你知道PrimeFaces有内置的客户端验证吗?是的,但是因为我想显示“小”文本属性而不是消息,所以我认为使用JS会更容易。1:它是一个小的“元素”(不是属性)2:使用类似的东西是不常见的(使用css进行样式设置)3:使用未定义/不清楚的id进行必要的验证是很奇怪的。。。有效地使这个不必要的复杂的不清楚的解决方案。好的,谢谢!基本上,我只想在按下submit时检查输入内容,并显示相应的消息。你不觉得应该这么难吗?PrimeFaces客户端验证就是这么做的
#emailError1{
    display: none;
}
function showErrorMessages() {
        document.getElementById("emailError1").style.display = "inline";
}