android eclipse 中的区分大小写(大写-小写)检查密码并确认密码字段

Case sensitivity (Uppercase-Lowercase) check for password and confirm password fields in android eclipse

我正在编辑个人资料页面,我需要检查密码字段是否区分大小写并确认密码文件。我如何在 android 中实现它? android eclipse.

中的区分大小写(大写-小写)检查密码并确认密码字段
public class MainActivity extends Activity implements AsyncResponse {
            EditText oldPW,newPW,conformPW;
            Button save;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.activity_main);

                oldPW=(EditText)findViewById(R.id.oldpassword);
                newPW=(EditText)findViewById(R.id.newpassword);
                conformPW=(EditText)findViewById(R.id.conformpassword);
                save=(Button)findViewById(R.id.saveid);

                save.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {               



                    String old1= oldPW.getText().toString();
                    String new1= newPW.getText().toString();
                    String conform1= conformPW.getText().toString();
                    //validation for null entry in old,new and confirm passwrods
        //            if (old1.matches("")) {
        //                Toast.makeText(MainActivity.this, "Please enter the Old Password", Toast.LENGTH_SHORT).show();
        //                return;
        //            }
                    if (new1.matches("")) {
                        Toast.makeText(MainActivity.this, "Please enter the New Password", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (conform1.matches("")) {
                        Toast.makeText(MainActivity.this, "Please enter the Confirm Password", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    //old password confirmation
                    if (!isValidPassword(old1)) {
                        oldPW.setError("Invalid Password");
                    }

                    //validation for password matching confirmation
                    if(!isPasswordMatching(new1,conform1)){
                        conformPW.setError("New password and confirm password must be the same!");
                    }
                    //validation for password not less than 6 characters
                    if(!isValid(new1)){
                        newPW.setError("Invalid Password");;
                    }

                    String key1 = "saasvaap123";
                    String signupid1 = "26";


                    String url = "http://gooffers.in/omowebservices/index.php/webservice/Public_User/change_user_pwd?";
                    CustomHttpClient task = new CustomHttpClient();
                    task.execute(url,key1,signupid1,old1,new1);
                    task.delegate = MainActivity.this;


                    }
                    });



                    } //oncreate close
            //old password validation
         // validating password with retype password
            private boolean isValidPassword(String old1) {
                if (old1 != null && old1.length() > 6) {
                    return true;
                }
                return false;
            }


            //validation for new and confirm password matching
            public boolean isPasswordMatching(String new1, String conform1) {
                Pattern pattern = Pattern.compile(new1, Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(conform1);
                if (matcher.matches() == true) {
                    //DO nothing
                } else {
                    // do your Toast("passwords are not matching");
                    Toast.makeText(MainActivity.this,"Passwords does not match", Toast.LENGTH_SHORT).show();
                }

                return matcher.matches();
            }

          //password not less than 6 characters
            public boolean isValid(String new1) {
                // check for null or too short
                if (new1 != null&&new1.length() > 6) {
                //  Toast.makeText(MainActivity.this,"Type atleast six characters", Toast.LENGTH_SHORT).show();
                    return true;
            }
                return false;
            }
            private class CustomHttpClient extends AsyncTask<String, String, String>{




                public AsyncResponse delegate=null;
                private String msg; 




         @Override
         protected void onPostExecute(String result) {
             // TODO Auto-generated method stub
             super.onPostExecute(result);

             delegate.processFinish(result);

         }

         @Override
         protected void onPreExecute() {
             // TODO Auto-generated method stub
             super.onPreExecute();
         }



                 @Override
                 protected String doInBackground(String... params) {
                     if(params == null) return null;

                     // get url from params
                     String url = params[0];
                     String key1 = params[1];
                     String signupid1 = params[2];

                     String old1 = params[3];
                     String new1 = params[4];


                     ArrayList<NameValuePair> postParameters;


                     postParameters = new ArrayList<NameValuePair>();
                     postParameters.add(new BasicNameValuePair("old_password",old1));
                     postParameters.add(new BasicNameValuePair("password",new1));
                     postParameters.add(new BasicNameValuePair("key",key1));
                     postParameters.add(new BasicNameValuePair("signup_id",signupid1));




                     try {
                         // create http connection
                         HttpClient client = new DefaultHttpClient();
                         HttpPost httppost = new HttpPost(url);
                         httppost.setEntity(new UrlEncodedFormEntity(postParameters));

                         // connect
                         HttpResponse response = client.execute(httppost);

                         // get response
                         HttpEntity entity = response.getEntity();

                         if(entity != null){
                             return EntityUtils.toString(entity);
                         }
                         else{
                             return "No string.";
                         }
                      }
                      catch(Exception e){
                          return "Network problem";
                      }
                 }




             }



               public void processFinish (String output){

                   Toast.makeText(MainActivity.this,output, Toast.LENGTH_SHORT).show();




                   try{   

                          JSONObject json=new JSONObject(output);
                        //  Integer success = json.getInt(SUCCESS);
                          String msg = json.getString("message");



                                  String a="Check old password";

                          if(msg.compareTo(a)==0){

                           Toast.makeText(MainActivity.this,"Your password has been successfully updated!", Toast.LENGTH_SHORT).show();

                         //  startActivity(new Intent(Login.this, HomeScreen.class));


                          }

                          else{

                                Toast.makeText(MainActivity.this,"Please enter the details correctly!", Toast.LENGTH_SHORT).show();



                          }



                    }catch (JSONException e) {

                    }       

                      }



        }
if(etConfirmPassword.getText().toString().equals(etPassword.getText().toString()))
{
    //Password Match
}
else
{

}

如果需要,您可以在字符串上使用 .trim()。

.equalsIgnoreCase()忽略字符串的大小写敏感度,.equals()在编译两个字符串时考虑字符大小写敏感度

public boolean isPasswordMatching(String new1, String conform1) 
{
       if(new1.equals(conform1))
       {
           return true;
       }
       Toast.makeText(MainActivity.this,"Passwords does not match", Toast.LENGTH_SHORT).show();
       return false;
}

如果您不希望密码区分大小写,请在存储时将密码转换为小写或大写。