I have a EditText
in my application that i need to check if the text is like,
##-##-##
the "#" can be letters or numbers in groups of 2, how can i do that?
I have a EditText
in my application that i need to check if the text is like,
##-##-##
the "#" can be letters or numbers in groups of 2, how can i do that?
You can use this regex
:-
String regex = "[a-zA-Z0-9]{2}-{1}[a-zA-Z0-9]{2}-{1}[a-zA-Z0-9]{2}";
System.out.println(str.matches(regex));
You should convert the text that user entered and run a regular expression check on it.
Here is a tutorial for regular expressions in Java by Sun.
To get the text from the EditText
field and convert it to string use the following:
EditText editText = (EditText)findViewById(R.id.your_edit_field_id);
String message = editText.getText().toString();
This check when the text is changed !
EditText etSearch this.etSearch = (EditText) findViewById(R.id.id_edittext);
etSearch.addTextChangedListener(new TextWatchControl());
private class TextWatchControl implements TextWatcher {
@Override
public void afterTextChanged(Editable s) {
String test = etSearch.getText().toString();
if (test.matches("[0-9]{2}-{1}[0-9]{2}-{1}[0-9]{2}""))
//true
else
//false
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}