I have two jsp forms in which I want to share the same validator class. The problem: form1 uses all three form inputs (firstName,lastName,email) but form2 only uses two form inputs (firstName,lastName). form1 works fine but form2 will give me an error because it is expecting an email input. What is the best way to test for the email input so as to skip the email validation code?
thanks in advance
thanks in advance
Code:
import java.util.*;
public class FormBean {
private String firstName;
private String lastName;
private String email;
private Hashtable errors;
public boolean validate() {
boolean allOk=true;
if (firstName.equals("")) {
errors.put("firstName","Please enter your first name");
firstName="";
allOk=false;
}
if (lastName.equals("")) {
errors.put("lastName","Please enter your last name");
lastName="";
allOk=false;
}
if (email.equals("") || (email.indexOf('@') == -1)) {
errors.put("email","Please enter a valid email address");
email="";
allOk=false;
}
return allOk;
}
public String getErrorMsg(String s) {
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
}
public FormBean() {
firstName="";
lastName="";
email="";
errors = new Hashtable();
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public void setFirstName(String fname) {
firstName =fname;
}
public void setLastName(String lname) {
lastName =lname;
}
public void setEmail(String eml) {
email=eml;
}
public void setErrors(String key, String msg) {
errors.put(key,msg);
}
}