Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block negative numbers on h:inputText with JSF

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?

like image 294
Chico Luiz Avatar asked Sep 24 '13 14:09

Chico Luiz


1 Answers

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.

like image 156
BalusC Avatar answered Oct 20 '22 15:10

BalusC