How to block negative numbers on h:inputText with JSF

Chico Luiz picture Chico Luiz · Sep 24, 2013 · Viewed 7.6k times · Source

I´m using jsf to build a form, and I have this kind of inputtext:

<div class="profile-info-row">
    <div class="profile-info-name">Promoção:</div>
    <div class="profile-info-value">
        <span class="input-icon input-icon-right">
            <h:inputText id="promocao" class="input-medium"
                value="#{itemController.itemPreco.promocao}"
                converterMessage="Valor inválido. (Ex.: 0.00)">
                <f:convertNumber locale="pt"/>
                <f:ajax event="change" render="promocaoMensagem"/>
            </h:inputText>
        </span>
        <div class="help-inline mensagemErro">
            <h:message for="promocao" id="promocaoMensagem"/>
        </div>
    </div> 
</div>

and I have to block negative numbers and give a message to user inline that number is forbidden, how is working when the user types a invalid number on input..

Does someone know how I can do that?

Answer

BalusC picture BalusC · Sep 24, 2013

Use <f:validateLongRange> with a minimum of 0.

<h:inputText ...>
    <f:validateLongRange minimum="0" />
</h:inputText>

The default validation message is:

Validation Error: Value is less than allowable minimum of '0'

Any custom validation message can be set via validatorMessage the same way as you did for converterMessage.