I have an editText field for which I want check that it meets with certain criteria, basically it has to contain 2ints and 3 strings, how can I check this before adding the element.
EditText.getText().toString();
could I add a check that does comething like this
public boolean checkString(String StringPassed) {
String s = StringPassed;
if(s.length == 5){
boolean hasString = false;
boolean hasInt = false;
String letters = s.substring(0, 2);
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher(letters);
if (m.matches()) {
hasString = true;
}
String numbers=s.substring(2,5);
try {
int num = Integer.parseInt(numbers);
String n = num + "";
if (num > 0 && n.length() == 3)
hasInt = true;
} catch (Exception e) {
}
if (hasInt && hasString) {
return true;
}
}else{
return false;
}
return false;
}
I then have a method which will say
public void addString() {
String StringPassed = EditTextName.getString().toString();
checkString(String StringPassed);
if (StringPassed() == false) {
Display Toast;
}
else {
add;
}
}
Also you can add a
TextWatcheron youredittextto listen for the text input change and call your method automatically