jsf inputtext中没有空格要求

use*_*896 2 javascript validation jquery jsf trim

我使用带有一些输入的jsf表单.需要一些输入.我通过required ="true"来查看空白区域.但是我需要在提交表单之前检查一下如果用户只是从键盘输入了一些间隙空格键,那么因为该值存储在数据库中,但我不需要它.我认为它可能是使用qjuery方法trim()实现的,但我是jquery的新手,我不知道如何实现它.

<h:form prependId="false">
    <div class="row">
        <div class="col-lg-3 form-group">
            <label>First Name<span class="text-danger"> *</span></label>
            <h:inputText value="#{userController.user.firstName}" 
                styleClass="form-control" id="firstName"
                required="true" requiredMessage="#{bundle.common_error_empty_value}"/>
            <h:message for="firstName" styleClass="validationMsg"></h:message>
        </div>
        <div class="col-lg-5 form-group">
            <label>Last Name:<span class="text-danger"> *</span></label>
            <h:inputText value="#{userController.user.lastName}" 
                styleClass="form-control" id="lastName"
                required="true" requiredMessage="#{bundle.common_error_empty_value}"/>
            <h:message for="lastName" styleClass="validationMsg"></h:message>
        </div>
    </div>
   //etc...
</h:form>
Run Code Online (Sandbox Code Playgroud)

我试过这样的事.也许有人知道如何检查白色空间上的字段.

<script type="text/javascript">
            function trimInputs() {
                $('form input').each(function () {
                    $.trim(value);
                });
            }
        </script>
Run Code Online (Sandbox Code Playgroud)

Xtr*_*ica 7

如果你使用Hibernate验证器,它可以使用@ mabi的解决方案,这也在这里解释.否则,您也可以编写自己的JSF验证器:

@FacesValidator
public class NoBlankSpaceValidator implements Validator{

    @Override
    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {
        //Check if user has typed only blank spaces
        if(value.toString().trim().isEmpty()){
            FacesMessage msg = 
                new FacesMessage("Incorrect input provided", 
                        "The input must provide some meaningful character");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并将其用作:

<h:inputText value="#{userController.user.firstName}" 
    required="true" requiredMessage="#{bundle.common_error_empty_value}"
    validator="noBlankSpaceValidator"/>
Run Code Online (Sandbox Code Playgroud)

也可以看看: