【问题标题】:Unable to call JS to validate (JSF)无法调用 JS 进行验证(JSF)
【发布时间】:2012-12-11 01:56:15
【问题描述】:

当用户按下提交时,我无法让它调用下面的 JS,我错过了什么

<script type="text/javascript"> 

        function required(){  
            if (document.getElementById("Username").value.length == 0)  
            {   
                alert("message");        
                return false;   
            }       
            return true;   
        }

    </script> 

用户输入用户名的代码

<h:form> <!--onsubmit="return username_validation();">-->


                <h:outputLabel for="username">Please enter your username: </h:outputLabel>
                <h:inputText id="Username" label="Username" value="#{user.id}"
                             size="20" required="true" requiredMessage="Please enter a username" >
                    <f:ajax event="blur" render="usernameMessage" />
                </h:inputText><br></br><br></br>
                <h:message id="usernameMessage" for="username" />
                To print a piece of text, please press the submit button below to upload the text:<br/><br/>
                <h:commandButton type="Submit" value="Submit" onclick="return username_validation()" action="upload/uploadText" styleClass="submit"/>

            </h:form>

感谢你们的帮助,我现在有了以下信息:但是我永远无法在h:inputText 旁边看到错误消息,我该怎么做?

 <h:form> <!--onsubmit="return username_validation();">-->


                    <h:outputLabel for="username">Please enter your username: </h:outputLabel>
                    <h:inputText id="Username" label="Username" value="#{user.id}"
                                 size="20" required="true" requiredMessage="Please enter a username" >

                    </h:inputText><br></br><br></br>

                    To print a piece of text, please press the submit button below to upload the text:<br/><br/>
                    <h:commandButton type="Submit" value="Submit" onclick="return username_validation()" action="upload/uploadText" styleClass="submit"/>

                </h:form>

这也是我的 userBean,我添加了@NotNull,但目前无法正常工作,我错过了什么吗?

package com.corejsf;

import java.io.Serializable;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.validation.constraints.NotNull;


@Named("user")
@SessionScoped
public class UserBean implements Serializable {

    @NotNull(message = "Please enter a username")
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String newValue) {
        id = newValue;
    }
    private String fileText;

    public String getFileText() {
        return fileText;
    }

    public void setFileText(String fileText) {
        this.fileText = fileText;
    }
}

【问题讨论】:

  • 渲染出来的 HTML 是什么样子的?
  • 嘿,我记得在引导程序中使用了一个功能:如果您为输入字段指定 required 属性,则必须填写它。不确定它是 html5 规范还是引导程序噱头。
  • 好的,谢谢,我会调查它,呈现的 HTML 看起来很好,没有错误,只是当我按下提交时,它会将用户带到下一页而不会出现错误

标签: javascript jsf


【解决方案1】:

查看生成的 HTML 源代码。在浏览器中右键单击页面并执行查看源代码。 HTML 源代码中根本不存在 ID 为 Username 的元素。相应地修复它。

<h:form id="form" ...>
    <h:inputText id="username" ... />
    ...
</h:form>

var usernameElement = document.getElementById("form:username");

与具体问题无关,纯粹通过 JavaScript 执行验证的方向是错误的。这是不可靠的。只需使用required="true"

<h:form>
    <h:inputText id="username" ... required="true">
        <f:ajax event="blur" render="usernameMessage" />
    </h:inputText>
    <h:message id="usernameMessage" for="username" />
    ...
</h:form>

另见:

【讨论】:

  • 谢谢你这些教程很棒,只是有两个简单的问题,我的错误消息永远不会出现在&lt;h:inputText 旁边我怎么能这样做,也在你的教程中你把@NotNull 放在userBean,我已经这样做了,但不起作用,我还需要在这里添加什么?
  • 我将renderupdate 属性混合在一起,我更新了答案。至于@NotNull,仅当 JSF 配置为将空提交的值解释为 null 时才有效。这也包含在链接的教程中。
  • 谢谢你,我现在要重新阅读它,我也更新了我的代码,但仍然在用户名所在的正文上方收到错误消息,有什么想法吗?
  • 你的教程很棒!,刚刚成功地做到了不为空
猜你喜欢
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
  • 2016-01-11
  • 2017-03-26
  • 1970-01-01
  • 2016-09-04
相关资源
最近更新 更多