How to Validate Password from Text Input in Android?
Whenever we type the password in most social sites or any web and android application, We get an alert to add a minimum of one lowercase alphabet, minimum one uppercase alphabet, minimum one numerical digit and length of password must be greater than or equal to 8. So In this article, we are going to implement the same.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > < EditText android:id = "@+id/addpasss" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "300dp" android:hint = "Write Password" /> < TextView android:id = "@+id/atoz" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:text = "a-z" android:textColor = "#000" android:textSize = "20dp" /> < TextView android:id = "@+id/AtoZ" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:text = "A-z" android:textColor = "#000" android:textSize = "20dp" /> < TextView android:id = "@+id/num" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:text = "0-9" android:textColor = "#000" android:textSize = "20dp" /> < TextView android:id = "@+id/charcount" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:text = "8 characters" android:textColor = "#000" android:textSize = "20dp" /> </ LinearLayout > |
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.graphics.Color; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity { EditText editText; TextView atoz, AtoZ, num, charcount; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initialise layout editText = findViewById(R.id.addpasss); atoz = findViewById(R.id.atoz); AtoZ = findViewById(R.id.AtoZ); num = findViewById(R.id.num); charcount = findViewById(R.id.charcount); // when we start typing editText.addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { // get the password when we start typing String password = editText.getText().toString(); validatepass(password); } @Override public void afterTextChanged(Editable editable) { } }); } public void validatepass(String password) { // check for pattern Pattern uppercase = Pattern.compile( "[A-Z]" ); Pattern lowercase = Pattern.compile( "[a-z]" ); Pattern digit = Pattern.compile( "[0-9]" ); // if lowercase character is not present if (!lowercase.matcher(password).find()) { atoz.setTextColor(Color.RED); } else { // if lowercase character is present atoz.setTextColor(Color.GREEN); } // if uppercase character is not present if (!uppercase.matcher(password).find()) { AtoZ.setTextColor(Color.RED); } else { // if uppercase character is present AtoZ.setTextColor(Color.GREEN); } // if digit is not present if (!digit.matcher(password).find()) { num.setTextColor(Color.RED); } else { // if digit is present num.setTextColor(Color.GREEN); } // if password length is less than 8 if (password.length() < 8 ) { charcount.setTextColor(Color.RED); } else { charcount.setTextColor(Color.GREEN); } } } |
Output:
Please Login to comment...