简体   繁体   中英

Validating Field Of Edittext in Android

when i am giving validation to field it does not validate proper. and when fields are blank it only shows the red mark not show error when i am clicking on edittext then it will display the error.

    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();
            }

        }
    });
}

try this my friend

        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
            }
        }
    });

change your xml controll like this

 <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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM