Actually I am trying to validate a registration form before submitting it.So after clicking the submit Button,Each field has to be validated and based on the validation results
You have to add else segment to remove setError like setError(null)
public class SignUpFragment extends Fragment implements View.OnClickListener {
EditText Signup_name,Signup_email,Signup_password,Signup_contact;
ImageView Signup_button;
Validation validation;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_sign_up, container, false);
Signup_name=(EditText)view.findViewById(R.id.SignUp_name);
Signup_email=(EditText)view.findViewById(R.id.SignUp_email);
Signup_password=(EditText)view.findViewById(R.id.SignUp_password);
Signup_contact=(EditText)view.findViewById(R.id.SignUp_number);
Signup_button=(ImageView)view.findViewById(R.id.SignUp_button);
Signup_button.setOnClickListener(this);
validation=new Validation();
return view;
}
@Override
public void onClick(View v) {
if (!validation.isValidName(Signup_name.getText().toString())) {
Signup_name.setError("please enter a name");
} else {
Signup_name.setError(null);
}
if (!validation.isValidEmail(Signup_email.getText().toString())) {
Signup_email.setError("please enter a Valid Email Address");
} else {
Signup_email.setError(null);
}
if (!validation.isValidEmail(Signup_password.getText().toString())) {
Signup_password.setError("min password length has to be 6");
} else {
Signup_password.setError(null);
}
if (!validation.isValidNumber(Signup_contact.getText().toString())) {
Signup_contact.setError("please enter a Valid Contact number");
} else {
Signup_contact.setError(null);
}
}
}
Here is my Validation.java
code:
public class Validation {
boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
boolean isValidPassword(String pass) {
if (!pass.isEmpty() && pass.length() >= 6) {
return true;
}
return false;
}
boolean isValidNumber(String num) {
if (!num.isEmpty() && num.length() == 10) {
return true;
}
return false;
}
boolean isValidName(String name) {
if (!name.isEmpty() && name.length() < 30) {
return true;
}
return false;
}
}