Phone number validation is an important functionality in all software application. Most of the application will use phone number as the unique id for identifying users . Mostly all application will send a otp to the mobile number to which the user entered.
So it is important to validate phone number which the user is providing. In this tutorial we will learn how to validate an phone number in android. If you are not familiar with auto verification process in android please refer How to send otp and verify mobile number automatically in android.
Steps
- Create new project in android studio
- Implement the layout file
- Implement the code for validating phone number in class file
Before start implementing example project let us learn about the function which is used for validating the mobile number. Following is the function which will check whether the mobile number is empty. Then it will check the pattern of the mobile number and returns a boolean value.
Function for phone number validation
public boolean isValidPhone(CharSequence phone) { if (TextUtils.isEmpty(phone)) { return false; } else { return android.util.Patterns.PHONE.matcher(phone).matches(); } }
Above function will validate the mobile number which is provided, now following code will show how to use the function.
if(isValidPhone(ed_phone.getText().toString())){ Toast.makeText(getApplicationContext(),"Phone number is valid",Toast.LENGTH_SHORT).show(); }else { Toast.makeText(getApplicationContext(),"Phone number is not valid",Toast.LENGTH_SHORT).show(); }
Example
Let us see a small example of phone number validation in android.
Create New project in android studio
Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio.
Implement the layout file
Now add the Following code to your layout, layout will contain an EditText to enter phone number and a Button to do validation.
<EditText android:id="@+id/ed_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/> <Button android:id="@+id/btn_validate_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Phone validation" android:background="@color/colorPrimary" android:textColor="#FFFFFF"/>
Implement the Java code for mobile validation
So we need to have the validation function which we learned above either in your activity file itself or you can place it in a static file if you have to use this function in multiple places.
public class Validator extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_validator); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); Button btn_validate_phone = (Button) findViewById(R.id.btn_validate_phone); final EditText ed_phone = (EditText) findViewById(R.id.ed_phone); btn_validate_phone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(isValidPhone(ed_phone.getText().toString())){ Toast.makeText(getApplicationContext(),"Phone number is valid",Toast.LENGTH_SHORT).show(); }else { Toast.makeText(getApplicationContext(),"Phone number is not valid",Toast.LENGTH_SHORT).show(); } } }); } public boolean isValidPhone(CharSequence phone) { if (TextUtils.isEmpty(phone)) { return false; } else { return android.util.Patterns.PHONE.matcher(phone).matches(); } } }
If you want to validation by giving limitation in number of digits also, replace the isValidPhone() function in the above code with following code
private boolean isValidPhone(String phone) { boolean check=false; if(!Pattern.matches("[a-zA-Z]+", phone)) { if(phone.length() < 6 || phone.length() > 13) { check = false; } else { check = true; } } else { check=false; } return check; }