@Pattern(regexp=".+@.+\\.[a-z]+",message="Username must be in e-mail format");
We define it just above the mailadress variable in Managedbean and life is so good. But inJSF1.2 we have to write our own mailValidator class. Here is how we can do it:
- First of all, create a validator class :/**
* @author Burhan ARAS
*
*/
public class EmailValidator implements Validator {
/* (non-Javadoc)
* @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
*/
@Override
public void validate(FacesContext facesContext, UIComponent uIComponent, Object object)
throws ValidatorException {
// TODO Auto-generated method stub
String enteredEmail = (String)object;
//Set the email pattern string
Pattern p = Pattern.compile(".+@.+\\\\.[a-z]+");
//Match the given string with the pattern
Matcher m = p.matcher(enteredEmail);
//Check whether match is found
boolean matchFound = m.matches();
if (!matchFound) {
FacesMessage message = new FacesMessage();
message.setDetail("Email geçersiz.");
message.setSummary("Kullanıcı adi e-mail formatında değil.");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
} - Secondly we should define this validator in faces-config.xml file. <validator>
<validator-id>emailvalidator</validator-id>
<validator-class>com.blog.util.EmailValidator</validator-class>
</validator> - And finally, we are gonna declare it in our preview file like html, jsf or whatever.
<p>Enter your email: <h:inputText value="email" id="email" required="true"> <f:validator validatorId="emailvalidator" /> </h:inputText> <h:message for="email" /></p>
Hiç yorum yok:
Yorum Gönder