验证失败

时间:2018-04-13 10:57:17

标签: java android xml

我在我的应用程序中处理android应用程序在该页面中有注册页面我正在使用@slackid,当用户填写注册表单并输入slack id而没有@其显示验证消息如@是必要的。比他搬到下一个。请告诉我如何使用这个@ validation消息。这是代码

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

5 个答案:

答案 0 :(得分:0)

尝试:

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) {

    }
});

如果您想要id应以@开头的内容,则可以使用此模式:^@.*

答案 1 :(得分:0)

您可以使用TextUtils.isEmpty(slackId.getText().toString())

进行空白验证

这将检查文本是否为空或

答案 2 :(得分:0)

您可以进行类似

的验证
if(!slackId.getText().toString().contains("@")){}

答案 3 :(得分:0)

在您的代码中,您进行了验证,如

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

这不会验证天气是否包含@。

这样做:

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

希望它能帮到你!!

答案 4 :(得分:0)

你可以使用它作为比你的案例使用textChange监听器更好的方法来在失去焦点后检查文本编辑,这将为你提供所需的验证而无需提交。

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