I'm trying to validate a TextView so that it can only contain alphabets and .
So far what I've done are it does not allow spaces and it cannot be left blank, so how do I carry on with not allowing numbers and special characters except for .
if (PatientInitials.getText().toString().equals(""))
{
final Toast toast = Toast.makeText(getApplicationContext(), "Please enter the patient initials.", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 3000);
}
else if (PatientInitials.getText().toString().matches(".*([ \t]).*"))
{
final Toast toast = Toast.makeText(getApplicationContext(), "Patient initials cannot contain spaces.", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 3000);
}
//Below doesn't work
else if (PatientInitials.getText().toString().matches("/^[A-z]+$/"))
{
final Toast toast = Toast.makeText(getApplicationContext(), "Patient initials cannot contain numbers and special characters.", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 3000);