JSF Number Validation

后端 未结 5 1916
独厮守ぢ
独厮守ぢ 2021-02-18 18:07

Is there any inbuilt number validator tag in JSF that checks whether an input entered in h:inputext field is a number?

The first question was answered. Edit

相关标签:
5条回答
  • 2021-02-18 18:41

    You can use f:convertNumber (use the integerOnly attribute).

    You can get more information here.

    0 讨论(0)
  • 2021-02-18 18:47

    You can use:

     <f:validateLongRange minimum="20" maximum="1000" />
    

    Where minimum is the smallest number allowed and maximum is the largest. Look here for more details

    0 讨论(0)
  • 2021-02-18 18:56

    i8taken solution converts number into long without validation message (at least in my case: JSF2 / global messages per page). For proper validation message you can
    1. check value in action method in bean;
    or
    2. use converter attribute for inputText:

    <h:inputText id="maxrecs" value="#{simpleBean.numRecords}" maxlength="4" converter="javax.faces.Integer" />

    0 讨论(0)
  • 2021-02-18 18:56

    You can simply use the passthrough, so first add this library

        xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
    

    and after use this

       <h:inputText id="numberId" pt:type="number" />
    
    0 讨论(0)
  • 2021-02-18 18:57

    JSF Number validation for inputtext mention f:converterNumber component in between h inputText component and mention the attributes integerOnly and type.

    <h:inputText id="textMobileId" label="Mobile" styleClass="controlfont" value="#{UserRegistrationBean.textMobile}">
        <f:convertNumber integerOnly="true" type="number" />
    </h:inputText>
    

    If you enter abcd in Mobile textbox at the time when you click on commandbutton it automatically shows an error like

    Mobile: 'abcd' is not a number. 
    
    0 讨论(0)
提交回复
热议问题