1

I am an android Beginner,

I want to validate phone numbers with special character + , ( , ) , - and all digits

example (123)456-7894

I tried this code

public boolean validCellPhone(String number){
        boolean b=true;

        String MOBILE_PATTERN = "[(,),-][0-9]{10,16}";    // ddm validating

       pattern = Pattern.compile(MOBILE_PATTERN);  
       matcher = pattern.matcher(cellph.getText().toString());  
       if (!matcher.matches()) { 


           cellph.requestFocus();
         b=false;
       }  
       else{

           b=true;
       }         
       return b;
}
Sukan
  • 346
  • 1
  • 3
  • 19

1 Answers1

1

Try this..

public boolean validCellPhone(String number){

   return android.util.Patterns.PHONE.matcher(number).matches();
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • Best trick I found,set android:inputType="number" in xml, it won't allow user to enter any special character like @, #% at all. – shaby Feb 03 '16 at 10:45