How to check the format of a phone number

I'm going to create a confirmation for the phone number format. The format is 10 digits including the plus sign, for example: +0133999504. Even though I declare the pattern I am trying to disallow, the "-" character or any other characters, but the validation does not work. 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();
                    }

      

+3


source to share


4 answers


Why not remove all the digits and then count the digits to the left and add the plus again? This allows users to freely fill in their phone number anyway ...

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 application is intended for international use, replace

if (!PhoneDigits.length()!=10) 

      

from

if(PhoneDigits.length() < 6 || PhoneDigits.length() > 13)

      

as Fatty Khan suggested.




To apply this in the code posted on Android EditText Validation and Regex , first include this method in your public class or the class that contains 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 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() );

        ...

      

+4


source


^[\\+]\\d{3}\\d{7}$

      

Use bindings to constrain compliance.



^

=> match start

$

=> end of match

+3


source


Given the rules you specified:

up to length 13 and including the + infront character.

(and also including a minimum length of 10 in your code)

You will need a regex that looks like this:

^\+[0-9]{10,13}$

      

With the minimum and maximum length encoded in a regular expression, you can remove these conditions from the if () block.

Problem: I would assume that the 10-13 range is too restrictive for the international phone number field; you will almost certainly find real numbers that are longer and shorter than this. I would advise you 8 to 20 people to be safe.

[EDIT] The OP claims that the above regex doesn't work because of the escape sequence. Not sure why, but an alternative would be:

^[+][0-9]{10,13}$

      

[EDIT 2] The OP now adds that the + sign should be optional. In this case, the regex needs a question mark after the +, so the above example now looks like this:

^[+]?[0-9]{10,13}$

      


For a valid mobile phone, you need to consider between 7 digits and 13 digits, because some country has a 7-digit mobile phone number. Also, we cannot check how the mobile number should start with 9 or 8 or nothing ..

For the mobile number I used this function

private boolean isValidMobile(String phone2) 
{
    boolean check;
    if(phone2.length() < 6 || phone2.length() > 13)
    {
        check = false;
        txtPhone.setError("Not Valid Number");
    }
    else
    {
        check = true;
    }
    return check;
}

      

+2


source


using this one for my phone number validation and it works like a charm. It covers phone verification and more that developers would like to try with a phone number.

+1


source







All Articles