0

我目前正在为android中的应用程序进行注册。我想捕获在 EditText 框中输入的无效手机号码。单击提交按钮后,应用程序将检查输入的手机号码是否有效。

所有有效数字都应以"639". 我的问题是如何读取用户输入的前三位数字?例如,用户输入639158716271,这是一个有效数字。虽然197837281919无效。

谁能建议如何做到这一点?

提前致谢!

4

6 回答 6

5
     //Get the string from the edit text by:
     String number = yourEditText.getText().toString();

     if(number != null && number.matches("639[0-9]{9}"))
     //do what you need to do for valid input
     else
     //do what you need to do for invalid input

matches()确保整个字符串(准确地)与它所采用的正则表达式相一致。639[0-9]{9}表示字符串必须以 639 开头,然后紧跟 9 位数字 (0-9)。例如,如果您想匹配"639"后跟 7 到 9 个数字,您可以使用:639[0-9]{7,9}。正则表达式:http ://docs.oracle.com/javase/tutorial/essential/regex/

于 2013-05-15T04:48:58.017 回答
5

Method 1:

Get an instance of the EditText:

EditText myEdit = (EditText) findViewById(R.id.edittext1);

Then get the string that is currently being displayed:

String phoneNumber = myEdit.getText().toString();

If its only initial number that you want to match, then you can just compare as follows:

String initialPart = phoneNumber.substring(0, 4);  
//Get 1st three characters and then compare it with 639
boolean valid = initialPart.equals("639");

Then you can continue making other comparisons. However this method is prone to some mistakes and you might miss some corner case. So I suggest to go for method 2:

Method:2

However one another very good way is to use Google's libphonenumber library. The documentation says:

It is for parsing, formatting, storing and validating international phone numbers. The Java version is optimized for running on smartphone.

I have used it for verifying phone numbers. It is very easy to use and you don't need to take care of the corner cases. It takes into account your country/location and all sorts of formats that the user may enter. It checks if the number is valid for that region. It also takes care of all possible valid format that user may enter like: "+xx(xx)xxxx-xxxx", "+x.xxx.xxx.xxx","+1(111)235-READ" ,"+1/234/567/8901", "+1-234-567-8901 x1234" ( here x is number).

Here is a sample usage of how to validate it:

PhoneNumber NumberProto = null;
String NumberStr = "639124463869"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
  NumberProto = phoneUtil.parse(NumberStr, "CH"); 
} catch (NumberParseException e) {
  System.err.println("NumberParseException was thrown: " + e.toString());
}
boolean isValid = phoneUtil.isValidNumber(NumberProto); // returns true or false

P.S: "CH" is the country code for Switzerland. You can enter your country code based on your need. They are given here. Hope it helps.

于 2013-05-15T05:04:43.390 回答
0

编辑:这是一个更长的解决方案。如果您真正需要的是验证史蒂夫指出的数字,那么正则表达式是缩短代码的方法。

===

首先,将您的 EditText 限制为仅接受数字,然后您可以使用以下内容来验证数字的有效性。

public static final int NUMBER_LENGTH = 9; // This will be used for validation

/**
 * Returns the prefix of a phone number
 */
public String getNumberPrefix(String number) {
    if (number != null) {
        if (number.length() > NUMBER_LENGTH) {
            return number.substring(0, number.length() - NUMBER_LENGTH);
        }
    }
    return "";
}

/**
 * Checks if a phone number is valid based on length and prefix
 */
public boolean isValidNumber(String number, String prefix) {
    if (prefix == null) {
        prefix = "";
    }
    if (number != null) {
        if (number.length() > 0) {
            if ((number.length() == NUMBER_LENGTH + prefix.length()) && (getNumberPrefix(number).equals(prefix))) {
                return true;
            }
        }
    }
    return false;
}

因此,在您的情况下,您可以设置一个恒定的数字长度:

使用该方法的一些示例:

isValidNumber("639158716271", "639"); // This will return true
isValidNumber("631111111111", "639"); // This will return false
isValidNumber("6391587", "639"); // This will return false
isValidNumber("123456789000", "639"); // This will return false
于 2013-05-15T04:57:22.443 回答
0
<EditText
                android:id="@+id/EditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:maxLength="12"
                 />

    int number = Intiger.ParseInt(EditText.getText.toString());

    if(number.startsWith("639")&& number.length == 12) 
    {
        //valid
    }
    else
    {
        //invalid
    }
于 2013-05-15T05:15:18.490 回答
0

您可以startsWith在字符串对象中使用该方法:

String num = "639158716271"
if (num.startsWith("639") && num.length() == 12)
  // valid
else
  // invaid
于 2013-05-15T04:54:38.153 回答
0

检查android强文本中的有效手机号码 添加您的XML Edittext

            android:maxLines="1"
            android:maxLength="10"
            android:inputType="number"
于 2021-01-21T10:23:06.093 回答