如何验证电话号码格式

kin*_*aya 5 regex validation android

我即将创建电话号码格式验证。格式为 10 位数字,包括加号,例如:+0133999504。尽管我已经声明了模式,但我尝试禁止使用“-”符号或任何其他字符,但验证不起作用。还有其他想法或解决方案吗?

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();
                    }
Run Code Online (Sandbox Code Playgroud)

小智 5

为什么不删除所有非数字,然后计算剩下的数字,然后再将加号放回去?这使得用户可以自由地填写他们想要的电话号码......

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  

}
Run Code Online (Sandbox Code Playgroud)

如果您的应用程序供国际使用,请替换

if (!PhoneDigits.length()!=10) 
Run Code Online (Sandbox Code Playgroud)

if(PhoneDigits.length() < 6 || PhoneDigits.length() > 13)
Run Code Online (Sandbox Code Playgroud)

正如法蒂汗建议的那样。


要在您在Android EditText Validation 和 Regex中发布的代码中应用此方法,请首先将此方法包含在您的公共类或包含 onClick() 的类中:

public boolean validateNumber(String S) {
    String Regex = "[^\\d]";
    String PhoneDigits = S.replaceAll(Regex, "");
    return (PhoneDigits.length()!=10);
}
Run Code Online (Sandbox Code Playgroud)

并在 CreateNewRider 类中包含此方法:

protected String tidyNumber(String S) {
    String Regex = "[^\\d]";
    String PhoneDigits = S.replaceAll(Regex, "");
    String Plus = "+";
    return Plus.concat(PhoneDigits);
}
Run Code Online (Sandbox Code Playgroud)

这是验证发生的地方......

@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();
}
Run Code Online (Sandbox Code Playgroud)

然后在 CreateNewRider 类中使用 tidyNumber() :

    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() );

        ...
Run Code Online (Sandbox Code Playgroud)