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

格雷巴尔布

我正在尝试对我的注册表进行一些客户端验证。它应该只是停止提交并显示一些文本来帮助用户了解出了什么问题。

这是我的 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>

JS

var email = document.getElementById("emailInput");

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

CSS:

#emailError1{
    display: none;
}

计划是触发 Primeface 验证(不能为空)并使描述文本内联。然而,我不确定如何做到这一点。现在我只是试图将它连接到提交命令按钮,但 id 更容易做到更早,这也将起作用。

提前致谢!

格雷巴尔布

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Node.js进行客户端与客户端的通信

使用Vue 2进行客户端表单验证

使用MVC进行客户端验证

使用HttpClient进行客户端身份验证

使用Blazor进行客户端验证

使用Dust.js进行客户端模板化的基本示例

在AJAX请求之前单击按钮进行客户端验证

如何在PHP / HTML中进行客户端表单数据验证

使用jax-rs和cxf进行客户端身份验证

使用JAX-WS进行客户端应用程序身份验证

使用AAD进行客户端身份验证仍需要证书

使用javascript文本框进行客户端验证

使用jQuery成功进行客户端验证后,如何提交html表单以进行服务器端验证?

使用nodejs和pug进行客户端模板

使用jquery,剔除进行客户端数据过滤

Android-使用FirebaseRecyclerAdapter进行客户端排序

使用jhipster 5.1.0进行客户端测试失败

在按钮(未提交)上单击或在填写表格时在YII中进行客户端验证

使用Pusher.com API进行客户端到客户端事件

在Jetty服务器中,当需要进行客户端身份验证时,如何获取使用的客户端证书?

在没有Spring Boot的Spring中使用证书进行客户端身份验证

使用证书,证书订单列表或默认证书进行客户端身份验证

使用django-oauth-toolkit在Django rest框架驱动的App中进行客户端身份验证

使用Angular.js和iOS客户端对Node.js应用程序进行身份验证

Scala.js与Scala GWT进行客户端Web开发?

在 REST 节点 js 服务器中进行身份验证,使用带有 Facebook 登录的 Android 客户端

是否有使用QUICKFIX库进行客户端与客户端通信的现成解决方案?

JS客户端表单验证-当通过eventListener的“提交”事件的formValidation函数返回false时,表单仍会提交

使用pngcrush之类的JavaScript进行客户端PNG压缩?