Avaoiding fractional numbers in JSF

Posted on 2009-04-28
Last Modified: 2013-11-24
I have a JSF form where i will be providing values in the text boxes. The text box should accept only Whole numbers. It should not accept non-numeric values like alphabets, special characters and also it should not accept "fractions". Only positive numbers should be allowed.

how this can be done?
Question by:Kadhiravan
    LVL 20

    Accepted Solution

    Does this help you?

    They add a Integer Only Validator to the desired Field.
    LVL 15

    Expert Comment

    You can use a bean to do that.This is what we did
    The example is for phone number but you can use the same for whole numbers
     <t:inputText id="daytimePhone" value="#{customer.daytimePhone}"        required="true" forceId="true">
                       <t:message style="color:red" for="daytimePhone" />
                        <f:validator validatorId="phoneNumberValidator"/>
    In the faces config file
    This is the bean
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import javax.faces.application.FacesMessage;
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.validator.Validator;
    import javax.faces.validator.ValidatorException;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    public class PhoneNumberValidator implements Validator
        private static final Log log = LogFactory.getLog(PhoneNumberValidator.class);
        private static final String PHONE_NUMBER_PATTERN = "[0-9]{3}[-]{1}[0-9]{3}[-]{1}[0-9]{4}";
        public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
            /* create a mask*/
            Pattern mask = Pattern.compile(PHONE_NUMBER_PATTERN);
            /* retrieve the string value of the field*/
            String phoneNumber = (String)value;
            /*check to ensure that the value is a phone number*/
            Matcher matcher = mask.matcher(phoneNumber);
                FacesMessage message = new FacesMessage();
                message.setDetail("Phone number not in valid format");
                message.setSummary("Phone number not in valid format");
                throw new ValidatorException(message);

    Open in new window


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now