0

I have an EditText here called "idCustomerCode", which used to input a code ID from user, which MUST contains alphanumeric on every input, for example ("abc123"), and will show error messages if it doesn't meet the criteria. (example : not contains alphabet / numeric ("abcd" or "1234" only), or contains symbol). I have no idea how to implement it into Android Studio.

Examples of Error Message popup :

Customer Code must only contain text and number

Can anyone help me with this code?

Thanks!

public class registerForm extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register_form);

    final EditText idUsername;
    final EditText idCustomerCode;
    final EditText idPass;



    idUsername = (EditText) findViewById(R.id.idUsername);
    idCustomerCode = (EditText) findViewById(R.id.idCustomerCode);
    idPass = (EditText) findViewById(R.id.idPass);

    Button btnRegister = (Button)findViewById(R.id.btnRegister);

    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent;
            intent = new Intent(getApplicationContext(),loginForm.class);
            intent.putExtra("idUsername", idUsername.getText().toString());
            intent.putExtra("idCustomerCode", idCustomerCode.getText().toString());
            intent.putExtra("idPass", idPass.getText().toString());
            startActivity(intent);
        }
    });

}

}

By the way, I also add this code to my xml, so the idCustomerCode EditText only accept alphanumeric :

android:digits="0123456789qwertzuiopasdfghjklyxcvbnm"

2 Answers2

0

Use this xml:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textCapCharacters"
    android:textAllCaps="true" />
MMG
  • 3,226
  • 5
  • 16
  • 43
0

Let your Activity (or Fragment) have fields with the acceptable values:

private String alphabet = "qwertzuiopasdfghjklyxcvbnm";
private String numbers = "0123456789";

Then you can iterate over the String to check if it contains the requested characters. Call this method in onClick() and only start the next Activity if it returns true:

private boolean isInputValid(@NonNull String input){
    String lowerCaseInput = input.toLowerCase();
    int length = input.length();
    boolean foundAlphabetValue = false;
    boolean foundNumericValue = false;
    for(int i = 0; i < length; i++){
        char c = lowerCaseInput.charAt(i);
        if(alphabet.contains(c)){
            foundAlphabetValue = true;
        }
        else if (numbers.contains(c)){
            foundNumericValue = true;
        } 
        else{
            return false;
        }
        return foundNumericValue && foundAlphabetValue; 
    }
}




 @Override
 public void onClick(View view) {
    if(isInputValid(idCustomerCode.getText().toString())){        
        Intent intent;
        intent = new Intent(getApplicationContext(),loginForm.class);
        // ...
        startActivity(intent);
    }
    else {
        String errorMessage = "Customer Code must only contain text and number";
        Toast.makeText(view.getContext(), errorMessage, Toast.LENGTH_SHORT).show();
    }
}
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61