Validate Hardcoded Password

Issue

I am using Kotlin.

I have a stored password (val password = 1111). I have an editText (passcode) that takes a "numberPassword" input. I would like the user to input the code and if it matches I will do something.

I have tried:

if (passcode.equals(1111)) { //do something }

if (passcode = password) { //do something }

if (passcode.text.toString() == password) { //do something }

if (passcode == "1111") { //do something }


and many other versions switching back and forth from int to string

This should be very simple since the user is either right or wrong. I am guessing it has to do with string/int stuff. Thanks for your help!

Just Tried:

    <EditText
        android:id="@+id/passcode"
        android:layout_width="120dp"
        android:layout_height="75dp"
        android:layout_gravity="center_horizontal"
        android:textAlignment="center"
        android:hint="@string/zeros"
        android:drawableStart="@drawable/ic_key"
        android:inputType="numberPassword"
        android:maxLength="4"
        android:maxLines="1"
        android:textColor="@color/black"
        android:textColorHint="@color/light_gray"
        android:textCursorDrawable="@null"
        android:textSize="35sp"
        android:singleLine="true"
        android:autofillHints="true" />

private var passcode: EditText? = null private var password: Int = 1111

        view.submitButton?.setOnClickListener {
          
            if (passcode?.text.toString() == password.toString()){


                Toast.makeText(this@SplashScreen, "Valid", Toast.LENGTH_LONG).show()
       } else {
                Toast.makeText(this@SplashScreen, "Invalid", Toast.LENGTH_LONG).show()
               
            }
        }

Result: Invalid when I enter 1111 in edittext

Solution

Your password variable is not initialized as it is still null.

Set it like this:

private var passcode : EditText? = findViewById<EditText>(R.id.passcode)

Answered By – ToxicFlame427

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *