1

so I made a password with some validations, the first thing I made was validation on the passittord edittext, when I entered the password a red close icon will appear and display a messase error, after the password has been filled the close icon still hasn't changed to the green check icon, when the password is filled with letters, big letters, and numbers, then the icon that was closed turned into a sucess or checklist icon, can friends help me solve this problem?

I made this still displays the close icon

enter image description here

this is code

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputPassword"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="20dp"
    android:hint="Password"
    app:counterEnabled="true"
    app:counterMaxLength="6"
    app:endIconMode="clear_text"
    app:endIconDrawable="@drawable/ic_canceles"
    app:errorEnabled="true"
    app:endIconTint="#DF0000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputEmail">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:maxLines="1"
        android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

this is code kotlin

submitButtom.setOnClickListener {

        if (editTextPassword.text.toString().length < 6) {
            editTextPassword.setError("password minimum contain 6 character")
            editTextPassword.requestFocus()
            editTextPassword.isEnabled = true

        }
        if (editTextPassword.text.toString().length > 6) {
            editTextPassword.setError("password maximum contain 6 character")
            editTextPassword.requestFocus()
        }
        if (editTextPassword.text.toString().equals("")) {
            editTextPassword.setError("please enter password")
            editTextPassword.requestFocus();
        }

    }
hashtag
  • 83
  • 2
  • 8
  • 1
    Here is [an example](https://howtodoinjava.com/regex/how-to-build-regex-based-password-validator-in-java/) of what you should look into. I apologize to future individuals if the link may break, but it's a broad topic. I recommend the OP also check around various sites for the different ways to set it up. – Tim Hunter Apr 01 '20 at 14:17
  • 1
    Use [Reg-Ex](https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a) for password validation . – ADM Apr 01 '20 at 14:17
  • thank you for helping me to get a reference@ADM – hashtag Apr 01 '20 at 14:26

3 Answers3

0

When are you calling editTextPassword.setError(null) to reset error-state visiblity?

Add TextWatcher to your EditText, check inside afterTextChanged that error-state is set, if is - reset (call setError(null)). You can also make live-validating after every letter change, not only after button pressed.

For validating proper password with proper characters use regexp like other answers suggests (I've misunderstanded your question a bit...)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • yes I tried to display the close icon and the success icon, how can I display the success icon if all conditions are passed@snachmsm – hashtag Apr 01 '20 at 14:25
  • I don't know if this state (green checkmark) is built-in. If not I would go to live switching [error color](https://stackoverflow.com/questions/33709066/how-to-set-textinputlayout-error-message-colour) and [drawable](https://stackoverflow.com/questions/37974299/change-style-of-the-right-icon-error-textinputlayout) of this field in `TextWatcher`s `afterTextChanged`. Still using `setError` method for showing text, even "positive" – snachmsm Apr 02 '20 at 09:35
0

Well I'm not going to test it, but you can achieve the requirements with regular expressions, in particular with forehead assertions, like this:

'^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[],;!?:._-]).{8,}$'

None of the forehead assertions consumes characters but they will look if the condition is met. The last part says there should be any character, but at least 8 of them. By the way, I put some special characters in the last regex class, you can put the ones you think must be allowed.

gnunez88
  • 13
  • 3
0

you can try with regex

val reg = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%!\-_?&])(?=\S+$).{8,}".toRegex()
val editTextPassword = "P@ssw0rd"
reg.matches(password)

will return true or false if your password matches the regex.

mvsg
  • 1
  • 3
  • ok, I will apply it, but if I want to display the success icon on the right if all the requirements are met, how can I, I can only display one close icon @user1748153 – hashtag Apr 01 '20 at 14:28
  • I got an error illegal escapse: '\-' and '\S' @ user1748153 – hashtag Apr 01 '20 at 14:34
  • this can't be-> val PASSWORD_PATTERN = Pattern.compile( "^(?=.*?[A-Z])" + "(?=.*?[a-z])" + ".{1,}\$").toRegex() val password = editTextPassword.text.toString().trim() if (!PASSWORD_PATTERN.matches(password)) { editTextPassword.setError("harus ada huruf besar dan huruf kecil") editTextPassword.requestFocus() } @user1748153 – hashtag Apr 01 '20 at 19:02