i am about to create a validation for phone number format..The format is 10 digit including the plus sign eg:+0133999504. Even though I have declare the pattern which is I try to disallow the "-" symbol or any other characters, but the validation is not working. Any other Idea or solution?
1st I declared the string regex:
String PhoneNo;
String PhoneNo_PATTERN ="[\\+]\\d{3}\\d{7}";
2nd I make a if..else statement:
 {
                    Pattern pattern = Pattern.compile(PhoneNo_PATTERN);
                    Matcher matcher = pattern.matcher(PhoneNo);
                    if (!matcher.matches()) 
                    {
                    inputemergencyContactNo.setError("Please enter Emergency Contact No");
                    }
                    else{
                    Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
                    }
                Why not remove all non-digits and then count the digits left and put the plus back in later? This allows users the freedom to fill out their phone number anyway they want...
String PhoneNo = "+123-456 7890";
String Regex = "[^\\d]";
String PhoneDigits = PhoneNo.replaceAll(Regex, "");
if (PhoneDigits.length()!=10)  
{
    // error message
}
else
{
    PhoneNo = "+";
    PhoneNo = PhoneNo.concat(PhoneDigits); // adding the plus sign
    // validation successful  
}
If your app is intended for international use replace
if (!PhoneDigits.length()!=10) 
with
if(PhoneDigits.length() < 6 || PhoneDigits.length() > 13)
as Fatti Khan suggested.
To apply this in the code you posted at Android EditText Validation and Regex first include this method in your public class or the class containing onClick():
public boolean validateNumber(String S) {
    String Regex = "[^\\d]";
    String PhoneDigits = S.replaceAll(Regex, "");
    return (PhoneDigits.length()!=10);
}
And include this method in the CreateNewRider class:
protected String tidyNumber(String S) {
    String Regex = "[^\\d]";
    String PhoneDigits = S.replaceAll(Regex, "");
    String Plus = "+";
    return Plus.concat(PhoneDigits);
}
This is where the validation happens...
@Override
public void onClick(View view) {
    Boolean b = false;
    if(inputfullname.getText().toString().equals("")) b = true;
    else if(... // do this for all fields
    else if(inputmobileNo.getText().toString().equals("")) b=true;
    else if(inputemergencyContactNo.getText().toString().equals("")) b=true;
    else {
        if(validateNumber( inputmobileNo.getText().toString() ) 
            Toast.makeText(RiderProfile.this, "Invalid mobile number", Toast.LENGTH_SHORT).show();
        else if(validateNumber( inputemergencyContactNo.getText().toString() ) 
            Toast.makeText(RiderProfile.this, "Invalid emergency contact number", Toast.LENGTH_SHORT).show();
        else {
            // Validation succesful
            new CreateNewRider().execute();
        }   
    }
    if(b) Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
}
And then use tidyNumber() in the CreateNewRider class:
    protected String doInBackground(String... args) {
        String fullname= inputfullname.getText().toString();
        String IC= inputIC.getText().toString();
        String mobileNo= tidyNumber( inputmobileNo.getText().toString() );
        String emergencyContactName= inputemergencyContactName.getText().toString() );
        String emergencyContactNo= tidyNumber( inputemergencyContactNo.getText().toString() );
        ...
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With