1

I have provision to enter phone number in an EditText.I want to make sure that the user enter the phone number in the format +91+phone number.ie, if user enter the first character, check is it a '+' then added to the EditText else not.like this i want to check for first 3 characters.How to do this?

editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                String phNum = charSequence.toString();
                if(phNum.trim() .length() > 0 ){
                    int length = phNum.trim().length();
                    switch (length) {
                    case 1:
                        if(phNum.substring(0) != null && !phNum.substring(0).equals("+")){
                            editText.setText("");
                        }
                        break;
                    case 2:
                        if(phNum.substring(0,2) != null && !phNum.substring(0,2).equals("+9")){
                            editText.setText(phNum.substring(0,1));
                        }
                        break;
                    case 3:
                        if(phNum.substring(0,3) != null && !phNum.substring(0,3).equals("+91")){
                            editText.setText(phNum.substring(0,2));
                        }
                        break;

                    default:
                        break;
                    }
                    Toast.makeText(CallFilterActivity.this, "Enter +91 before number", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Thanks in Advance

Devu Soman
  • 2,246
  • 13
  • 36
  • 57

2 Answers2

0

You could use the TextWatcher listener. eg.

editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {


        }

        @Override
        public void afterTextChanged(Editable s) {
            if(isFirstCharacter){
              //check if the character is + and add
             }else{

             }

        }
    });
Rajitha Siriwardena
  • 2,749
  • 1
  • 18
  • 22
0
Use something like this:

editText.addTextChangedListener(this);  
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stug
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub
}

Check for the entered character and then allow further typing or display an alert.