I want to validate the mobile number user enters. I have two edit texts one for the code i.e. +91,0 etc and another for the phone number.
I have a question that how to stop entering the numbers in edit text if more than 10 numbers being entered by the user. Also it should get validated with code and the number.
I tried the validation with this code.
private boolean isValidMobile(String phone)
{
return android.util.Patterns.PHONE.matcher(phone).matches();
}
else if (!isValidMobile(code.getText().toString()+mobileNo.getText().toString()))
{
Toast.makeText(RegisterActivity.this,"Please enter correct Mobile No.",Toast.LENGTH_LONG).show();
}
But it dose not return true for the number. Always returns false i.e. please enter the correct number.
edit texts for number :
<EditText
android:layout_width="30dp"
android:layout_height="match_parent"
android:ems="10"
android:id="@+id/editText_code"
android:layout_marginLeft="20dp"
android:background="@android:color/transparent"
android:hint="+91"
android:textSize="14sp"
android:phoneNumber="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="MOBILE NO"
android:singleLine="false"
android:layout_below="@+id/linearLayoutFirstName"
android:layout_toRightOf="@+id/linearLayoutFirstName"
android:layout_toEndOf="@+id/linearLayoutFirstName"
android:background="@android:color/transparent"
android:layout_gravity="center"
android:textSize="12sp"
android:layout_marginLeft="05dp"
android:id="@+id/mobileNo"
android:phoneNumber="true" />
</LinearLayout>
How to do this? Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…