×
☰ See All Chapters

JSF Validation

  • Validation of user input data is the crucial part of any web enterprise application. Developer should implement the validation for desired input forms. In servlet and JSP we implement the validation through JavaScript and for server side validation we use AJAX. 

  • JSF provides build support for server side validation and all validations implemented through JSF are server side validations. Also developer can implement client side validations using JavaScript. 

  • Validation of the user input happens during invoke validators phase of JSF life cycle. If validation is success, then submitted values are updated to Managed Bean. 

  • If validation fails, then the form is redisplayed with validation error messages. Validation error messages are specified using <h:message> tag. 

jsf-validation-0
 
  • JSF provides the below built in validators: 

    1. <f:validateLength/> 

    2. <f:validateLongRange/> 

    3. <f:validateDoubleRange/> 

    4. <f:validateRegex/> 

    5. <f:validator/> 

  • Along with the above built in validators JSF provides required attribute with input elements, which takes care of mandatory validation. If value for this attribute is true, then field becomes mandatory. 

<h:outputText value="Enter your name" />

<h:inputText id="nme" value="#{helloBean.name}" required="true"

        requiredMessage="PLEASE ENTER USERNAME"

        validatorMessage="USERNAME LENGTH SHOULD BE BETWEEN 3 AND 20">

        <f:validateLength minimum="3" maximum="20" />

</h:inputText>

<h:message for="nme" style="color:red" />


All Chapters
Author