Create a form that looks like the following: see image.
Make all fields required.
For the last name field, make sure at least two characters are entered.
For the birth date field, use the date pattern of MM/dd/yyyy.
For the confirm password field, use a custom validator the will check to make sure both passwords entered are the same.
In order to do this you will need to pass the password field to the validator. To do this you will need to use the bind attribute so the password is bound to an instance to the bean property. Here is the code for the password field
< h:outputLabel for="password" value="Password: " />
< h:inputSecret id="password" binding="#{password}"
value="#{myBean.password}"
required="true">
< /h:inputSecret>
< h:message for="password" styleClass="errorMessage" />
In this example, the backing bean is MyBean.java and it contains all the get and set methods for the fields displayed in the form.
You pass the password field as an attribute when you set the custom validator. If the custom validator is passwordValidator for the example, then below is the code for the confirm password field:
< h:outputLabel for="confirmpassword" value="Confirm Password: " />
< h:inputSecret id="confirmpassword"
value="#{myBean.confirmpassword}"
required="true">
< f:validator validatorId="passwordValidator" />
< f:attribute name="password" value="#{password" />
< /h:inputSecret>
< h:message for="confirmpassword" styleClass="errorMessage" />
In the java code for the validator you will need to get both password fields. Here is how do it:
public void validate(FacesContext context, UIComponent component, Object value) {
// Get the first password from the attribute
UIInput passwordComponent = (UIInput) component.getAttributes().get("password");
// Get the value from the UIInput component
String password = (String) passwordComponent.getValue();
// Get the value entered in the second string from the
// component parameter passed to the method
String confirm = (String) value;
// compare fields
if(!password.equals(confirm)) {
throw new ValidatorException(new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"Passwords do not match!", null));
}
}
Also, create a result.xhtml page that displays the contents of the fields with a link to return to the index.xhtml file like this: see image.