如何在JSF页面中使用validateRegex验证数字字段?

如何在JSF页面中使用validateRegex验证数字字段?,regex,validation,jsf,Regex,Validation,Jsf,在托管bean中,我有一个int类型的属性 @ManagedBean @SessionScoped public class Nacharbeit implements Serializable { private int number; 在JSF页面中,我尝试仅对6位数字输入验证此属性 <h:inputText id="number" label="Auftragsnummer"

在托管bean中,我有一个int类型的属性

@ManagedBean
@SessionScoped
public class Nacharbeit implements Serializable {

private int number;
在JSF页面中,我尝试仅对6位数字输入验证此属性

               <h:inputText id="number"
                         label="Auftragsnummer"
                         value="#{myController.nacharbeit.number}"
                         required="true">
                <f:validateRegex pattern="(^[1-9]{6}$)" />
            </h:inputText>
正则表达式错了吗?或者ValidateRegex仅用于字符串?

仅用于
字符串
属性。但是有一个
int
属性,JSF在验证之前已经将提交的
String
值转换为
Integer
。这解释了您看到的异常

但是,由于您已经在使用
int
属性,当您输入非数字时,您将已经得到一个转换错误。转换错误消息可通过
converterMessage
属性进行配置。所以你根本不需要使用正则表达式

至于具体的功能需求,您似乎想要验证最小/最大长度。因此,您应该改为使用。将此选项与
maxlength
属性结合使用,这样最终用户就不能输入超过6个字符

<h:inputText value="#{bean.number}" maxlength="6">
    <f:validateLength minimum="6" maximum="6" />
</h:inputText>
您也可以在不使用regex的情况下实现这一点 要验证int值,请执行以下操作:

<h:form id="user-form">  
    <h:outputLabel for="name">Provide Amount to Withdraw  </h:outputLabel><br/>  
    <h:inputText id="age" value="#{user.amount}" validatorMessage="You can Withdraw only between $100 and $5000">  
    <f:validateLongRange minimum="100" maximum="5000" />  
    </h:inputText><br/>  
    <h:commandButton value="OK" action="response.xhtml"></h:commandButton>  
</h:form> 

提供提款金额

要验证浮点值,请执行以下操作:

<h:form id="user-form">  
    <h:outputLabel for="amount">Enter Amount </h:outputLabel>  
    <h:inputText id="name-id" value="#{user.amount}" validatorMessage="Please enter amount between 1000.50 and 5000.99">  
    <f:validateDoubleRange minimum="1000.50" maximum="5000.99"/>  
    </h:inputText><br/><br/>  
    <h:commandButton value="Submit" action="response.xhtml"></h:commandButton>
</h:form>  

输入金额


<h:form id="user-form">  
    <h:outputLabel for="name">Provide Amount to Withdraw  </h:outputLabel><br/>  
    <h:inputText id="age" value="#{user.amount}" validatorMessage="You can Withdraw only between $100 and $5000">  
    <f:validateLongRange minimum="100" maximum="5000" />  
    </h:inputText><br/>  
    <h:commandButton value="OK" action="response.xhtml"></h:commandButton>  
</h:form> 
<h:form id="user-form">  
    <h:outputLabel for="amount">Enter Amount </h:outputLabel>  
    <h:inputText id="name-id" value="#{user.amount}" validatorMessage="Please enter amount between 1000.50 and 5000.99">  
    <f:validateDoubleRange minimum="1000.50" maximum="5000.99"/>  
    </h:inputText><br/><br/>  
    <h:commandButton value="Submit" action="response.xhtml"></h:commandButton>
</h:form>