public class ClassPathResource {
public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
public static boolean isMobile(Editable mobile) {
return Pattern.matches(REGEX_MOBILE, mobile);
}
public static boolean isPassword(Editable password) {
return Pattern.matches(REGEX_PASSWORD, password);
}
public static boolean isEmail(Editable email) {
return Pattern.matches(REGEX_EMAIL, email);
}
}
public class MainActivity extends AppCompatActivity {
EditText tel,pwd,email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tel=findViewById(R.id.tel);
pwd=findViewById(R.id.pwd);
email=findViewById(R.id.email);
tel.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.length()==11) {
boolean mobile = ClassPathResource.isMobile(s);
Toast.makeText(MainActivity.this, mobile + "", Toast.LENGTH_SHORT).show();
}
}
});
pwd.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.length()>6) {
boolean password = ClassPathResource.isPassword(s);
Toast.makeText(MainActivity.this, password + "", Toast.LENGTH_SHORT).show();
}
}
});
email.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
boolean email = ClassPathResource.isEmail(s);
Toast.makeText(MainActivity.this, email+"", Toast.LENGTH_SHORT).show();
}
});
}
}