Animesh Jena
Animesh Jena

Reputation: 1551

Errors with registration form Validation using setError() in Android

Actually I am trying to validate a registration form before submitting it.So after clicking the submit Button,Each field has to be validated and based on the validation results if not properly filled,an error message will be visible thanks to SetError().But here is the problem,After filling the correct data also,that error icon with the message is still there.please help.

here is my code for the Signupform.xml file:-

    <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/Signup"
            android:textColor="#686868"
            android:textSize="25dp"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginTop="20dp"
            android:background="@mipmap/login_name_input"
            android:hint="Enter your name"
            android:maxLines="1"
            android:inputType="text"
            android:id="@+id/SignUp_name"
            android:singleLine="true"
            android:focusable="true"
            android:clickable="true"
            android:focusableInTouchMode="true"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginTop="20dp"
            android:background="@mipmap/login_name_input"
            android:hint="Enter your Email Address"
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:id="@+id/SignUp_email"
            android:singleLine="true"
            android:focusable="true"
            android:clickable="true"
            android:focusableInTouchMode="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Select your Country"
            android:textColor="#676767" />

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginTop="20dp"
            android:entries="@array/Country_name">

        </Spinner>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <EditText
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                android:background="@mipmap/login_name_input" />

            <EditText
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:layout_weight="4"
                android:background="@mipmap/login_name_input"
                android:hint="Enter your Contact Number"
                android:inputType="numberDecimal"
                android:maxLines="1"
                android:id="@+id/SignUp_number"
                android:singleLine="true"
                android:focusable="true"
                android:clickable="true"
                android:focusableInTouchMode="true"/>
        </LinearLayout>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginTop="20dp"
            android:background="@mipmap/login_name_input"
            android:hint="Enter your password"
            android:inputType="textPassword"
            android:id="@+id/SignUp_password"
            android:maxLines="1"
            android:singleLine="true"
            android:focusable="true"
            android:clickable="true"
            android:focusableInTouchMode="true"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:background="@mipmap/submit_btn"
            android:inputType="text"
            android:text="Sign Me Up"
            android:id="@+id/SignUp_button"
            android:textColor="@android:color/white"
            android:textStyle="bold" />


    </LinearLayout>
</ScrollView>


Here is my code for the SignupFragment.java:-


public class SignUpFragment extends Fragment implements View.OnClickListener {
    EditText Signup_name,Signup_email,Signup_password,Signup_contact;
    ImageView Signup_button;
    Validation validation;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_sign_up, container, false);
        Signup_name=(EditText)view.findViewById(R.id.SignUp_name);
        Signup_email=(EditText)view.findViewById(R.id.SignUp_email);
        Signup_password=(EditText)view.findViewById(R.id.SignUp_password);
        Signup_contact=(EditText)view.findViewById(R.id.SignUp_number);
        Signup_button=(ImageView)view.findViewById(R.id.SignUp_button);
        Signup_button.setOnClickListener(this);
        validation=new Validation();
        return  view;
    }


    @Override
    public void onClick(View v)
    {
        if(!validation.isValidName(Signup_name.toString()))
        {

            Signup_name.setError("please enter a name");
        }
        if(!validation.isValidEmail(Signup_email.toString()))
        {

            Signup_email.setError("please enter a Valid Email Address");
        }
        if(!validation.isValidEmail(Signup_password.toString()))
        {
            Signup_password.setError("min password length has to be 6");
        }
        if(!validation.isValidNumber(Signup_contact.toString()))
        {

            Signup_contact.setError("please enter a Valid Contact number");
        }
    }
}


Here is my Validation.java code:-

    ublic class Validation
{
    boolean isValidEmail(String email) {
        String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
    boolean isValidPassword(String pass) {
        if (!pass.isEmpty() && pass.length() >= 6) {
            return true;
        }
        return false;
    }
    boolean isValidNumber(String num)
    {
        if (!num.isEmpty() && num.length()==10) {
            return true;
        }
        return false;
    }
    boolean isValidName(String name)
    {
        if (!name.isEmpty() && name.length()<30) {
            return true;
        }
        return false;
    }

}

Upvotes: 0

Views: 1939

Answers (2)

Arun Shankar
Arun Shankar

Reputation: 622

You have to add else segment to remove setError like setError(null)

public class SignUpFragment extends Fragment implements View.OnClickListener {
    EditText Signup_name,Signup_email,Signup_password,Signup_contact;
    ImageView Signup_button;
    Validation validation;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_sign_up, container, false);
        Signup_name=(EditText)view.findViewById(R.id.SignUp_name);
        Signup_email=(EditText)view.findViewById(R.id.SignUp_email);
        Signup_password=(EditText)view.findViewById(R.id.SignUp_password);
        Signup_contact=(EditText)view.findViewById(R.id.SignUp_number);
        Signup_button=(ImageView)view.findViewById(R.id.SignUp_button);
        Signup_button.setOnClickListener(this);
        validation=new Validation();
        return  view;
    }


    @Override
    public void onClick(View v) {
        if (!validation.isValidName(Signup_name.getText().toString())) {
            Signup_name.setError("please enter a name");
        } else {
            Signup_name.setError(null);
        }

        if (!validation.isValidEmail(Signup_email.getText().toString())) {
            Signup_email.setError("please enter a Valid Email Address");
        } else {
            Signup_email.setError(null);
        }

        if (!validation.isValidEmail(Signup_password.getText().toString())) {
            Signup_password.setError("min password length has to be 6");
        } else {
            Signup_password.setError(null);
        }

        if (!validation.isValidNumber(Signup_contact.getText().toString())) {
            Signup_contact.setError("please enter a Valid Contact number");
        } else {
            Signup_contact.setError(null);
        }
    }
}

Here is my Validation.java code:

public class Validation {
    boolean isValidEmail(String email) {
        String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    boolean isValidPassword(String pass) {
        if (!pass.isEmpty() && pass.length() >= 6) {
            return true;
        }
        return false;
    }

    boolean isValidNumber(String num) {
        if (!num.isEmpty() && num.length() == 10) {
            return true;
        }
        return false;
    }

    boolean isValidName(String name) {
        if (!name.isEmpty() && name.length() < 30) {
            return true;
        }
        return false;
    }
}

Upvotes: 3

MEGHA RAMOLIYA
MEGHA RAMOLIYA

Reputation: 1917

    private EditText edt_firstName,edt_email;
     private String firstName,email;

    edt_firstName = findViewById(R.id.edt_firstName);
    edt_email = findViewById(R.id.edt_email);

     private void validateData(){
     firstName = edt_firstName.getText().toString().trim();
     email = edt_email.getText().toString().trim();
    if (!firstName.isEmpty() && !email.isEmpty() && isValidEmail(email) {
    //here api call of the register user.
    }else{
    if (firstName.isEmpty()) {
                    edt_firstName.setError("Please Enter First Name");
                    edt_firstName.requestFocus();
                }
     else if (email.isEmpty()) {
                    edt_email.setError("Please Valid Email Id");
                    edt_email.requestFocus();
                } else if (!isValidEmail(email)) {
                    edt_email.setError("Please Enter Valid Email");
                    edt_email.requestFocus();
                }
    }
    }

private static boolean isValidEmail(String email) {
        return !TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

Upvotes: 0

Related Questions