如何对以下代码执行摘要验证?

时间:2013-07-27 11:02:38

标签: android validation login validationsummary

enter image description here

if((noValidate.getText().toString().length()>0)  &&     (nameValidate.getText().toString().length()>0)  &&
            (conpassValidate.getText().toString() ==  passValidate.getText().toString()) &&  (email.matches(emailPattern))
            &&(passValidate.getText().toString().length()>0))
        {
        Toast.makeText(this, "Your Information are saved succesfully ",   Toast.LENGTH_SHORT).show();

        Intent getStarted = new Intent(getApplicationContext(),  FourthActivity.class);
        startActivity(getStarted);
        }
    else
    {
        Toast.makeText(this, "Field Requiered",Toast.LENGTH_SHORT ).show();
    }
}

在这里,当我点击登录按钮时,它将验证并夏令营。如果任何字段为空,则会显示所需的字段。如果我在所有字段中输入数据意味着它始终显示所需的字段。我不知道问题出在哪里。

1 个答案:

答案 0 :(得分:1)

在Java中,您无法使用相等的运算符(=)来比较字符串:

conpassValidate.getText().toString() ==  passValidate.getText().toString()

改为使用equal方法:

conpassValidate.getText().toString().equals(passValidate.getText().toString())

这将是完整的代码:

if((noValidate.getText().toString().length()>0)  &&     
   (nameValidate.getText().toString().length()>0)  &&
   (conpassValidate.getText().toString().equals(passValidate.getText().toString())) &&  
   (email.matches(emailPattern)) && 
   (passValidate.getText().toString().length()>0)) {

       Toast.makeText(this, "Your Information are saved succesfully ",   Toast.LENGTH_SHORT).show();
       Intent getStarted = new Intent(getApplicationContext(),  FourthActivity.class);
       startActivity(getStarted);
} else {
       Toast.makeText(this, "Field Requiered",Toast.LENGTH_SHORT ).show();
}

如果仍然无效,请在其他地方使用此代码打印验证

String tag = "Your Tag";
Log.d(tag, "1: " + noValidate.getText().toString().length()>0 ? "YES":"NO");
Log.d(tag, "2: " + nameValidate.getText().toString().length()>0 ? "YES":"NO");
Log.d(tag, "3: " + conpassValidate.getText().toString().equals(passValidate.getText().toString()) ? "YES":"NO");
Log.d(tag, "4: " + email.matches(emailPattern) ? "YES":"NO");
Log.d(tag, "5: " + passValidate.getText().toString().length()>0 ? "YES":"NO");