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