2017-06-26 119 views
0

當我給驗證,以IT領域不驗證正確。 當字段爲空時,它只顯示紅色標記不顯示錯誤,當我點擊edittext時,它會顯示錯誤。驗證領域EDITTEXT Android中

nameValidate = "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$"; 
    mobile = "^[2-9]{2}[0-9]{8}$"; 
    passwordpattern = "((?=.*[a-z])(?=.*\\d)(?=.*[A-Z])(?=.*[@#$%!]).{8,40})"; 

    //registration button code 
    btn_registration = (Button) findViewById(R.id.buttonRegister); 
    btn_registration.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      uname = edittext_username.getText().toString(); 
      password = edittext_Password.getText().toString(); 
      phone = edittext_phone.getText().toString(); 
      if (uname.equals("") || uname.length() <= 3 || uname != nameValidate) { 
       if (uname.equals("")) { 
        edittext_username.setError("Name cannot be Blank"); 
       } else { 
        edittext_username.setError("Enter Valid Name"); 
       } 
      } 


      //Toast.makeText(getApplicationContext(), "Please enter username upto 6char", 1000).show(); 

      if (password.equals("") || password.length() < 3) { 
       // Toast.makeText(getApplicationContext(), "please enter password upto 6char ", 1000).show(); 
       edittext_Password.setError("Password cannot be blank"); 

      } 

      if (phone.equals("") || phone.length() < 10) { 
       edittext_phone.setError("Invalid mobile number"); 
       //Toast.makeText(getApplicationContext(), "please entermobile number must be 10 digit", 1000).show(); 
      } 
      if (uname == null && uname.equals(nameValidate) || password == null || phone == null) { 
       confirmOtp(); 
      } 

     } 
    }); 
} 
+0

其Android版本你測試嗎? –

回答

0

這個嘗試我的朋友

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

      if(phone.getText().toString().length() <10){ 
       phone.setError("Please enter valid phone number"); 
       phone.requestFocus(); 
      } 
      if(phone.getText().toString().trim().equals("")){ 
       phone.setError("Please enter phone number"); 
       phone.requestFocus(); 
      } 
      if(pass.getText().toString().trim().length() <6){ 
       pass.setError("password must contain 6 character"); 
       pass.requestFocus(); 
      } 
      if(pass.getText().toString().trim().equals("")){ 
       pass.setError("Please enter pass "); 
       pass.requestFocus(); 
      } 

      if(username.getText().toString().trim().equals("")){ 
       username.setError("Please enter user name"); 
       username.requestFocus(); 
      } 

      if(!username.getText().toString().trim().equals("")&& 
        !pass.getText().toString().trim().equals("")&& 
        pass.getText().toString().length() ==10 && 
        !phone.getText().toString().trim().equals("")&& 
        phone.getText().toString().length() ==10){ 
       // do your action here your validation are complete 
      } 
     } 
    }); 

更改XML控制研究這樣

<EditText 
    android:id="@+id/username" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " 
    android:gravity="center" 
    android:hint="user name" 
    android:maxLines="1" /> 

<EditText 
    android:id="@+id/pass" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:hint="password" 
    android:inputType="textPassword" 
    android:maxLines="1" /> 

<EditText 
    android:id="@+id/phone" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:hint="phone number" 
    android:inputType="number" 
    android:maxLength="10" 
    android:maxLines="1" /> 
+0

它的工作原理,但它專注於密碼字段沒有用戶名和領域,我需要驗證字符串,如果用戶輸入錯誤的串像他當時串輸入符號驗證 – Yogesh

+0

@Yogesh等待我會更新我的答 –

+0

好先生感謝反饋 – Yogesh