I'm writing a password regular expression at Android Studio. We received numbers, English characters, and special characters through the check box, and set the font color to be different depending on whether the conditions are met. By the way, if I check several check boxes, it says it doesn't fit unconditionally, so how should I modify it? When pwC1 and pwC2 are checked at the same time, it does not come out as desired if the conditions are met.
String pwChar = edit_Age.getText().toString();
String pwC1 = "^[0-9]*$";
String pwC2 = "^[a-z]*$";
PWc=edit_Age.getText().toString();
boolean is_pw1 = Pattern.matches(pwC1, pwChar);
boolean is_pw2 = Pattern.matches(pwC2, pwChar);
switch (v.getId()) {
case R.id.checkBox:
if (is_pw1){
edit_Age.setTextColor(Color.parseColor("#00ff33"));
Toast.makeText (MainActivity.this, "Recommended Password", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText (MainActivity.this, "Unrecommended Password", Toast.LENGTH_SHORT).show();
edit_Age.setTextColor(Color.parseColor("#ff0000"));
}
break;
case R.id.checkBox2:
if (is_pw2){
edit_Age.setTextColor(Color.parseColor("#00ff33"));
Toast.makeText (MainActivity.this, "Recommended Password", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText (MainActivity.this, "Unrecommended Password", Toast.LENGTH_SHORT).show();
edit_Age.setTextColor(Color.parseColor("#ff0000"));
}
break;
Hello. :-)
You want to use pwC1 as a number and pwC2 as a condition.
Java is not the main language, but I would do this.
String pwC1 = isChecked1==true? "0-9" :"; // Check No. 1
String pwC2 = isChecked2 == True? "a-z":"; // Checked No. 2
String pwPattern = "^[" +pwC1+pwC2+"]*$";
Please check if the pwChar you entered matches the pwPattern.
Note that the condition of pwC2 does not match the condition of pwC2. In other words, only lowercase letters meet the requirements. If you want to include capital letters, you can correct it to A-z
.
I feel this every time I use the p.s regular expression, but... It's more delicate and tricky than I thought, and it seems like I'm getting stressed out in an unexpected place. I'm outside the code after testing and validating in places like https://regexr.com. I recommend you to try it because it can reduce the time and effort required for the test.
Thank you.
© 2024 OneMinuteCode. All rights reserved.