0

I am using JSF2.0, Spring 3 and Hibernate 4.

Is it possible in jsf to enter text or characters in inputText in either upper case or lower case? I mean even if user enters in upper case, inputText should always accept characters in lower case.

Is this possible in JSF 2?

Thanks

Jacob
  • 14,463
  • 65
  • 207
  • 320
  • 1
    can't you just call **String.toLowerCase()** in the backing bean if you want to just convert an uppercase character to lowercase ?? – PermGenError Jan 15 '13 at 09:40
  • @GanGnaMStYleOverFlowErroR Yes it is possible, was wondering whether in JSF any such client validations exists or not? – Jacob Jan 15 '13 at 09:46
  • 2
    In pure JSF you have to validate it in server side, as the first answer says. http://stackoverflow.com/a/4014119/1199132 If you want client-side validation you'll have to use your own javascript. – Aritz Jan 15 '13 at 10:02
  • However you can take a look to third-party component libraries as Primefaces or Richfaces, maybe you can find something already done. – Aritz Jan 15 '13 at 10:04

2 Answers2

2

You can implement your own validator to validate text case. Check this tutorial. But it's not client side validation - request will be sent to server and if it's not valid page will be reloaded and show validation error message.

Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
  • Actually, what he needs is a converter, a validator is used to fulfil a different need – kolossus Jan 16 '13 at 05:59
  • @kolossus it's probably true. But there is a possibility that someone wants to make users input only text in lower case, not convert it on server. You can't be sure. – Mikita Belahlazau Jan 16 '13 at 09:25
2

You could use a Converter to automatically transform the input; see the Java EE 6 tutorial for more.

@FacesConverter("lowerConverter")
public class LowerConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext cx, UIComponent component,
                              String value) throws ConverterException {
        if(value == null) {
            return null;
        }
        Locale locale = cx.getExternalContext().getRequestLocale();
        return value.toLowerCase(locale);
    }

    @Override
    public String getAsString(FacesContext cx, UIComponent component,
                              Object value) throws ConverterException {
        if(value == null) {
            return null;
        }
        return value.toString();
    }
}
McDowell
  • 107,573
  • 31
  • 204
  • 267