-4

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?

Celta
  • 3,540
  • 3
  • 21
  • 22

3 Answers3

3

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));
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

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();
Kjartan
  • 18,591
  • 15
  • 71
  • 96
tbkn23
  • 5,205
  • 8
  • 26
  • 46
0

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) {
        }


    }
VincentLamoute
  • 800
  • 1
  • 9
  • 16