1

I am working on android application in my application there are registration page in that page i am using @slackid, when user fill the registration form and enter slack id without @ its show validation message like @ is necessary. Than he move to the next. Kindly please tell me how i use this @ validation message. Here is the code

activity_registration.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".RegisterActivity">

    <EditText
        android:id="@+id/name_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Your Name"/>
    <EditText
        android:id="@+id/email_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Your email id"
        android:inputType="textEmailAddress"/>
    <EditText
        android:id="@+id/slackid_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint=" @slackId"/>
    <EditText
        android:id="@+id/password_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="Password"/>
    <EditText
        android:id="@+id/confirm_password_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="Retype Password"/>
    <EditText
        android:id="@+id/info_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Information/Phone no/Optional"/>

    <Button
        android:id="@+id/register_reg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="Register"/>

</LinearLayout>

RegistrationActivity.java

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText name,emailId,slackId,password,conPasword,info;
    private Button registerB;

    // Alert dialog
    AlertDialog.Builder alertBuilder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        name = findViewById(R.id.name_reg);
        emailId = findViewById(R.id.email_reg);
        slackId = findViewById(R.id.slackid_reg);
        password = findViewById(R.id.password_reg);
        conPasword = findViewById(R.id.confirm_password_reg);
        info = findViewById(R.id.info_reg);
        registerB = findViewById(R.id.register_reg);
        //set register to onClick event
        registerB.setOnClickListener(this);



    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.register_reg:
                // Check all requir field empty or not
                //Apply the validation in each field including slack Id
                if(name.getText().toString().length()==0) {
                    name.setError("Name cannot be blank");
                }
               if(emailId.getText().toString().equals("")) {
                    emailId.setError("Email cannot be blank");
                }
                if(String.valueOf(slackId.getText().toString().charAt(0)).equals("@")) {
                    slackId.setError("Slack id cannot be blank");
                }
               if (password.getText().toString().equals("")) {
                    password.setError("password cannot be blank");
                }
               if(conPasword.getText().toString().equals("")) {
                        conPasword.setError("confirm password cannot be blank");
                    // if any of the required field empty "Show Dialog to fill the required field
                    alertBuilder = new AlertDialog.Builder(RegisterActivity.this);
                    alertBuilder.setTitle("Something Wrong");
                    alertBuilder.setMessage("Please Fill all required field");
                    alertBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    AlertDialog alertDialog = alertBuilder.create();
                    alertDialog.show();
                }else if(!(password.getText().toString().equals(conPasword.getText().toString()))){

                    //check pasword and confirm pasword mismatch
                    alertBuilder = new AlertDialog.Builder(RegisterActivity.this);
                    alertBuilder.setTitle("Something Wrong");
                    alertBuilder.setMessage("Pasword Mismatch");
                    alertBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                            password.setText("");
                            conPasword.setText("");
                        }
                    });
                    AlertDialog alertDialog = alertBuilder.create();
                    alertDialog.show();
                }else{
                    // Background task to insert user information into database
                    BackgroundLoginTask backgroundLoginTask = new BackgroundLoginTask(RegisterActivity.this);
                    backgroundLoginTask.execute("register",name.getText().toString(),
                                                emailId.getText().toString(),
                                                slackId.getText().toString(),
                                                password.getText().toString(),
                                                info.getText().toString());
                }
                break;
        }
    }
}
Kunal Pitale
  • 25
  • 11
  • 1
    Possible duplicate of [How can I check if a single character appears in a string?](https://stackoverflow.com/questions/506105/how-can-i-check-if-a-single-character-appears-in-a-string) – AskNilesh Apr 13 '18 at 11:08

5 Answers5

0

Try :

edittext.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {

               if(!edittext.getText().toString().contains("@")) {
                     edittext.setError("@ not detected");
                 }
    }


    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    public void afterTextChanged(Editable s) {

    }
});

If you want something that the id should start with @ then can use this pattern : ^@.*

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
0

You can blank validation by using TextUtils.isEmpty(slackId.getText().toString())

This will check if text is null or empty

Ahmad Ayyaz
  • 774
  • 8
  • 25
0

You can make a validation like

if(!slackId.getText().toString().contains("@")){}
Harish Rawal
  • 226
  • 2
  • 15
0

In your code you did validation like

if(String.valueOf(slackId.getText().toString().charAt(0)).equals("@")) {
                    slackId.setError("Slack id cannot be blank");
                }

this will not validate weather is contains @ or not.

do this:

if(!slackId.getText().toString().contains("@")){
  //show your error message here
}

Hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
0

You can use this as its better than using textChange listener for your case to check the text edit after losing focus , which will give you the needed validation without submitting .

EditText ed= (EditText) findViewById(R.id.edittxt);

 ed.setOnFocusChangeListener(new OnFocusChangeListener() {          
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus && !ed.getText().toString().contains("@")) {
               ed.setError("@ not detected")
            }
        }
    });
Mohammad
  • 739
  • 7
  • 22