Home > Net >  Validate Hardcoded Password
Validate Hardcoded Password

Time:11-15

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

CodePudding user response:

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

Set it like this:

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

CodePudding user response:

You are comparing the whole widget to the variable password. What you need is to compare the password variable with passcode.text.toString() like this:

 if (passcode.text.toString() == password.toString()){
                Toast.makeText(this@MainActivity, "Correct Passcode!", Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this@MainActivity, "Wrong Passcode!", Toast.LENGTH_SHORT).show()
            }
  • Related