当我对字段进行验证时,它无法正确验证。 当字段为空时,只显示红色标记,当我点击edittext时显示错误,然后显示错误。
nameValidate = "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$";
mobile = "^[2-9]{2}[0-9]{8}$";
passwordpattern = "((?=.*[a-z])(?=.*\\d)(?=.*[A-Z])(?=.*[@#$%!]).{8,40})";
//registration button code
btn_registration = (Button) findViewById(R.id.buttonRegister);
btn_registration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uname = edittext_username.getText().toString();
password = edittext_Password.getText().toString();
phone = edittext_phone.getText().toString();
if (uname.equals("") || uname.length() <= 3 || uname != nameValidate) {
if (uname.equals("")) {
edittext_username.setError("Name cannot be Blank");
} else {
edittext_username.setError("Enter Valid Name");
}
}
//Toast.makeText(getApplicationContext(), "Please enter username upto 6char", 1000).show();
if (password.equals("") || password.length() < 3) {
// Toast.makeText(getApplicationContext(), "please enter password upto 6char ", 1000).show();
edittext_Password.setError("Password cannot be blank");
}
if (phone.equals("") || phone.length() < 10) {
edittext_phone.setError("Invalid mobile number");
//Toast.makeText(getApplicationContext(), "please entermobile number must be 10 digit", 1000).show();
}
if (uname == null && uname.equals(nameValidate) || password == null || phone == null) {
confirmOtp();
}
}
});
}
答案 0 :(得分:1)
试试这个我的朋友
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(phone.getText().toString().length() <10){
phone.setError("Please enter valid phone number");
phone.requestFocus();
}
if(phone.getText().toString().trim().equals("")){
phone.setError("Please enter phone number");
phone.requestFocus();
}
if(pass.getText().toString().trim().length() <6){
pass.setError("password must contain 6 character");
pass.requestFocus();
}
if(pass.getText().toString().trim().equals("")){
pass.setError("Please enter pass ");
pass.requestFocus();
}
if(username.getText().toString().trim().equals("")){
username.setError("Please enter user name");
username.requestFocus();
}
if(!username.getText().toString().trim().equals("")&&
!pass.getText().toString().trim().equals("")&&
pass.getText().toString().length() ==10 &&
!phone.getText().toString().trim().equals("")&&
phone.getText().toString().length() ==10){
// do your action here your validation are complete
}
}
});
像这样更改你的xml控件
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:gravity="center"
android:hint="user name"
android:maxLines="1" />
<EditText
android:id="@+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="password"
android:inputType="textPassword"
android:maxLines="1" />
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="phone number"
android:inputType="number"
android:maxLength="10"
android:maxLines="1" />