Java 如何在android应用程序中验证电话号码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19649785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate phone number in android application?
提问by Amrutha
I am working on android. In my app users will register by entering their phone number in edittext. This phone number is then saved in database. Now when the user login with the app, I am getting the list of contacts from his mobile and comparing that with the people who register with this app. If the number in the contacts list matches with the number in the database, then I need to display those numbers in listview. Here the problem is if the user save his number with +91 or with 0 before his contact then the number in the database is not matching with the contact. At that time the numbers are not displaying.
我在安卓上工作。在我的应用程序中,用户将通过在 edittext 中输入他们的电话号码进行注册。这个电话号码然后被保存在数据库中。现在,当用户使用该应用程序登录时,我会从他的手机获取联系人列表,并将其与注册此应用程序的人进行比较。如果联系人列表中的号码与数据库中的号码匹配,那么我需要在列表视图中显示这些号码。这里的问题是,如果用户在他的联系人之前用 +91 或 0 保存他的号码,那么数据库中的号码与联系人不匹配。那个时候数字不显示。
For this issue, Do we need to keep any alert before entering the number in edit text? For example in edit text I gave, Ph no: 8923458128 and the saved it in database. Now I logged in with this number and my contacts list for suppose
对于这个问题,我们需要在编辑文本中输入数字之前保持警惕吗?例如,在我给出的编辑文本中,Ph no: 8923458128 并将其保存在数据库中。现在我用这个号码和我的联系人列表登录假设
- 9823484586
- +919988334856
- 9823484586
- +919988334856
Lets say the above 2 numbers are stored in database. But the 2nd contact , the user entered as 9988334856 without +91. Then finally in the listview instead of 2 numbers only 1 number is displaying as the second number is not matching with database number.
假设以上 2 个数字存储在数据库中。但是第二次联系时,用户输入了 9988334856 而没有 +91。然后最后在列表视图中而不是 2 个数字只显示 1 个数字,因为第二个数字与数据库编号不匹配。
How can I solve this issue? Please help me in this regard.
我该如何解决这个问题?请在这方面帮助我。
回答by RussVirtuoso
Use TEXT not Int in your database. When you use Int, if the first number is 0 it will disregard so just use TEXT.
在您的数据库中使用 TEXT 而不是 Int。当您使用 Int 时,如果第一个数字是 0,它将被忽略,因此只需使用 TEXT。
回答by Manish Srivastava
I think best way to do it create your table with 4 column-
我认为最好的方法是用 4 列创建表格 -
1)id 2)name(if needed) 3)country-code 4)phone number
1)身 2)姓名(如果需要) 3)国家代码 4)电话号码
And now on your UI prefix country code in a spiner and give phone-number type of field in a textview. And in your database use integer value to store number.
现在在微调器中的 UI 前缀国家/地区代码上,并在文本视图中提供电话号码类型的字段。并在您的数据库中使用整数值来存储数字。
And from matching your phone number just pass this query- 1)phoneNumber = phoneNumberEditText.getText().toString(); 2) // Reading all contacts from database
从匹配您的电话号码只需通过此查询 - 1)phoneNumber = phoneNumberEditText.getText().toString(); 2) // 从数据库中读取所有联系人
List<Contacts> number = db.getAllNumber();
for (final Contacts cn : number) {
if ((phoneNumber.equals(cn.getNumber()){
//do what you want
}
}
Thanks!Hope it will help you.
谢谢!希望它会帮助你。
回答by Pankaj Kumar
If you are fetching all phones from DB, then you can use below code to match entered phone with phone from DB using PhoneNumberUtils.compare()
如果您从数据库中获取所有电话,那么您可以使用以下代码使用PhoneNumberUtils.compare()将输入的电话与数据库中的电话进行匹配
It compares phone numbers a and b, return true if they're identical enough for caller ID purposes.
它比较电话号码 a 和 b,如果它们对于来电显示目的足够相同,则返回 true。
private String getMatchedPhones(ArrayList<String> contactsFromDB, String phoneToMatch) {
// Iterate all numbers and match
for (String numberFromDb : contactsFromDB) {
if (PhoneNumberUtils.compare(numberFromDb, phoneToMatch)) {
return phoneToMatch; // Or numberFromDb
}
}
return null; // Or can custom msg. If not matched.
}
回答by Shylendra Madda
Put these two lines in your XML file at that phone number edit text field
将这两行放在您的 XML 文件中的电话号码编辑文本字段中
android:inputType="numberDecimal"
android:maxLength="10"
then he could not enter more than 10 numbers and only he should enter numbers. you can take country code in one more text field and validate with it.
那么他不能输入超过10个数字,只有他应该输入数字。您可以在另一个文本字段中输入国家/地区代码并使用它进行验证。
回答by atul rathor
private boolean phvalidator(String ph2) {
私人布尔 phvalidator(字符串 ph2){
// TODO Auto-generated method stub
String expression = "^[0-9-1+]{10,15}$";
CharSequence inputStr = ph;
Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
return (matcher.matches())? true : false;
回答by Sunishtha Singh
try this
尝试这个
final String mobile = emobile.getText().toString();
if (!isValidMobile(mobile)){
emobile.setError("Invalid Mobile");
}
private boolean isValidMobile(String mobile) {
if (!TextUtils.isEmpty(mobile)) {
return Patterns.PHONE.matcher(mobile).matches();
}
return false;
}
回答by AMI CHARADAVA
For an Mobile number validation android provide InBuilt Patterns.But it only works in API level 8 and above.Try below one line code.
对于手机号码验证 android 提供 InBuilt Patterns。但它仅适用于 API 级别 8 及以上。尝试下面一行代码。
/* method for phone number validation */
private Boolean Number_Validate(String number)
{
return !TextUtils.isEmpty(number) && (number.length()==10) && android.util.Patterns.PHONE.matcher(number).matches();
}
you can call this mehtod by passing number in parameter,it return either true or false.
您可以通过在参数中传递数字来调用此方法,它返回 true 或 false。
Number_Validate(number);
Hope you get your answer. Thanks.
希望你能得到答案。谢谢。