Reputation: 65
java, B.java, C.java in all the three class I do the same validation for a particular string. Is it possible to write a single class and call it as a method in the class wherever its required, below is the validation for the mobile number which i use in the above three classes respectively.
A.java
mobile = (EditText) findViewById(R.id.app_mobile);
String ap_Mobile_No = mobile.getText().toString();
if (ap_Mobile_No.equals(null) || (ap_Mobile_No.length() != 10) || ap_Mobile_No.startsWith("0")){
mobile.setError("E");
result = false;
}
B.java
EditText mobile = (EditText) findViewById(R.id.gua_mobile);
String gu_Mobile_No = mobile.getText().toString();
if (gu_Mobile_No.equals("") || gu_Mobile_No.length() != 10 || gu_Mobile_No.startsWith("0")) {
mobile.setError(ssbuilder);
result = false;
}
Upvotes: 0
Views: 211
Reputation: 3026
Yes you can. Create a class lets say Validator.java create static method validate which returns boolean on the basis of success and failure. and call function any where as
Validatorn.validate(<arguments>)
so your class become
public final class Validator {
public static boolean validate(final EditText mobile) {
boolean isSuccess = true;
String gu_Mobile_No= mobile.getText().toString();
if (gu_Mobile_No.equals("") || gu_Mobile_No.length() != 10 || gu_Mobile_No.startsWith("0")) {
mobile.setError(ssbuilder);
isSuccess = false;
}
return isSuccess;
}
}
Now class A.java become
mobile = (EditText) findViewById(R.id.app_mobile);
if(Validator.validate(mobile)) {
return false;
}
and class B.java become
EditText mobile = (EditText) findViewById(R.id.gua_mobile);
if(Validator.validate(mobile)) {
return false;
}
Upvotes: 1
Reputation: 2578
Yes, a simple solution is to create a static method in a MobileValidator class. Static methods are just one way of doing it...
public class MobileValidator {
public static boolean isValid(EditText mobile) {
String gu_Mobile_No = mobile.getText().toString();
if (gu_Mobile_No.equals("") || gu_Mobile_No.length() != 10 || gu_Mobile_No.startsWith("0")) {
return false;
}
return true;
}
}
Then use this in your A/B classes
e.g.
boolean valid = MobileValidator.isValid(mobile);
if(!valid) {
// do something
}
Upvotes: 0
Reputation: 11
how about , create a class like below
public class Validate{
public static boolean ValidateString(String validateString){
return (validateString.equals("") || validateString.length() != 10 || validateString.startsWith("0"));
}
}
then , in your code ,you can
A.java
mobile = (EditText) findViewById(R.id.app_mobile);
String ap_Mobile_No = mobile.getText().toString();
if (Validate.ValidateString(ap_Mobile_No)==false)
{
mobile.setError("E");
result = false;
}
Upvotes: 0
Reputation: 91
Create a Utility class and implement the method for mobile number validation and invoke it wherever you want. Please ensure that you mark the method as static. this will avoid creation of new instances every time you validate the mobile number
Upvotes: 0
Reputation: 3130
You can create Class for this validation and call this class using Method;
public class Validation
{
public static boolean checkValidation(arguments)
{
check your validation here.
return true or false;
}
}
Upvotes: 2
Reputation: 68715
Yes you can write a single class for the validation purpose. Add the validator methods as static methods and call it from other classes.
Upvotes: 0