How to validate email in java?
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.validator.ValidatorException;
public void validateEmail (FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
String eMail = (String) value;
if (!"".equals(eMail)) {
if (eMail.indexOf("@") < 0) {
FacesMessage message = new FacesMessage("Invalid email address!");
throw new ValidatorException(message);
}
}
}
//Using the above method with primefaces input text field
<p:inputText id="memail" value="#{addmem.memail}" validator="#{user.validateEmail}" required="false" label="memail" size="35" />
How to validate uploaded file or check file existence in disk in java?
UploadedFile uFile = (UploadedFile) value;
String uploadfilename = uFile.getFileName().substring(uFile.getFileName().lastIndexOf("\\") + 1, uFile.getFileName().length());
File f = new File(serverfileaddress);
if (f.exists()) {
FacesMessage message = new FacesMessage("File already exist !");
throw new ValidatorException(message);
}
}
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.validator.ValidatorException;
public void validateEmail (FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
String eMail = (String) value;
if (!"".equals(eMail)) {
if (eMail.indexOf("@") < 0) {
FacesMessage message = new FacesMessage("Invalid email address!");
throw new ValidatorException(message);
}
}
}
//Using the above method with primefaces input text field
<p:inputText id="memail" value="#{addmem.memail}" validator="#{user.validateEmail}" required="false" label="memail" size="35" />
How to validate uploaded file or check file existence in disk in java?
import javax.faces.validator.ValidatorException;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent; import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent; import javax.faces.context.ExternalContext;
import org.primefaces.model.UploadedFile;
public void validateFile(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
UploadedFile uFile = (UploadedFile) value;
String uploadfilename = uFile.getFileName().substring(uFile.getFileName().lastIndexOf("\\") + 1, uFile.getFileName().length());
String serverfileaddress = servletContext.getRealPath("") + File.separator + testlabConstants.FOLDER_NAME_OF_UPLOADED_FILE + File.separator + uploadfilename;
File f = new File(serverfileaddress);
if (f.exists()) {
FacesMessage message = new FacesMessage("File already exist !");
throw new ValidatorException(message);
}
}
// Using the above method with primefaces file upload component
<p:fileUpload label="Upload File" id="uploadfile" validator="#{tdbean.validateFile}" required="true" value="#{tdbean.file}" mode="simple"/>
No comments:
Post a Comment