check numeric value in JSF
<ice:inputText id="txt-tlmanage-quantity"
value="#{createToolsOrderBean.toolsOrderVO.quantity}" tabindex="7"
onkeydown="moveFocus(event, 'txt-tlmanage-unitprice')"
style="margin-left: 4px;margin-bottom: 4px;">
</ice:inputText>
this page is submitted when i press button by calling createSomething
method. But this method can't call when i entered string value as quantity
is of type Integer.It will gives error backside, But how one can know , What's going to be wrong ?.
I use <ice:message>
, but it will give long error description on page.
Error Meaasage :
mainForm:txt-tlmanage-quantity: 'dsad' must be a number between -214748364开发者_开发问答8 and 2147483647 Example: 9346
Is there any way to print my own error message ?
You should add an integer converter to your ice:inputText. This will convert the entered string to integer.
<ice:inputText id="txt-tlmanage-quantity"
value="#{createToolsOrderBean.toolsOrderVO.quantity}" tabindex="7"
onkeydown="moveFocus(event, 'txt-tlmanage-unitprice')"
style="margin-left: 4px;margin-bottom: 4px;">
<f:converter converterId="javax.faces.Integer"/>
</ice:inputText>
You will still have an error message if you don't enter an integer. To display a custom message instead of the built-in you should create a message bundle. Create a properties file in one of your packages, and add your custom error message:
javax.faces.converter.IntegerConverter.INTEGER={2}: ''{0}'' must be a number consisting of one or more digits.
javax.faces.converter.IntegerConverter.INTEGER_detail={2}: ''{0}'' must be a number between -2147483648 and 2147483647 Example: {1}
Add this properties file as a resource bundle to the faces-config.xml
:
<faces-config>
<application>
<message-bundle>my.package.mypropertiesfile</message-bundle>
</application>
</faces-config>
First, edit and paste the error, it will help us to help you.
If you want to show a custom error, use FacesMessage
Example:
public void testingErrorMessages() {
try {
throw new Exception("");
} catch(Exception exc) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "Error message here!");
FacesContext.getCurrentInstance().addMessage(null, facesMsg);
}
}
Just add it and update the <ice:message>
. reRender
for RichFaces and update
for PrimeFaces, but I don't know how it works in IceFaces..
I think your problem is that he is not telling you that you may not use any letters?
You'll fix this, by using e.g. a converter.
Just add the following snippet to your inputText attributes:
converter="javax.faces.Integer"
When you gonna submit now, you will receive an error at your . If you want to create custom error messages use an validator. You'll find a good tutorial here.
Use converter attribute in jsf for accepting only numeric values
If you want to print your own error message in jsf then you can use converterMessage attribute.
<h:inputText id="textCreditCardNumberId" label="CreditCard Number"
converter="javax.faces.Integer" converterMessage="Please enter numeric only"
maxlength="16" styleClass="controlfont"
value="#{OnlineReservationBean.creditCardNumber}"></h:inputText>
精彩评论