Custom Password Validation UI in Lightning Web Component Salesforce | LWC Stack ☁️⚡️

 In this video I will show you how you can create a beautiful UI for a custom Password Validation in Lightning Web Component.

I found a very good example of it on W3 Schools and tried to implement it using LWC.

https://www.w3schools.com/howto/howto_js_password_validation.asp


What you will learn in this blog ? 

✅ Custom Validation UI

✅ Add/Remove CSS

✅ onfocus & onblur events

✅ Match text with REGEX


Please follow below code to implement it in a LWC : 

CSS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
.valid {
  color: green;
}
.valid:before {
  position: relative;
  left: -5px;
  content: "✔";
}
.invalid {
  color: red;
}
.invalid:before {
  position: relative;
  left: -5px;
  content: "✖";
}


HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<template>
  <lightning-card title="Password Validation">
    <div class="slds-var-m-around_medium">
      <lightning-input type="text" label="Username" required></lightning-input>
      <lightning-input
        type="password"
        label="Enter your password"
        name="password"
        pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
        required
        onchange={handleChange}
        onfocus={handleFocus}
        onblur={handleBlur}
      ></lightning-input>
      <br />
      <lightning-button label="Save" title="Save"> </lightning-button>
      <br />
      <template if:true={showValidation}>
        <div class="slds-box slds-theme_shade slds-theme_alert-texture">
          <h3>Password must contain the following :</h3>
          <p data-id="letter" class="invalid">A lowercase letter</p>
          <p data-id="capital" class="invalid">A uppercase letter</p>
          <p data-id="number" class="invalid">A number character</p>
          <p data-id="length" class="invalid">Minimum 8 characters</p>
        </div>
      </template>
    </div>
  </lightning-card>
</template>


JS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { LightningElement } from "lwc";

export default class PasswordValidation extends LightningElement {
  showValidation;
  handleFocus() {
    this.showValidation = true;
  }
  handleBlur() {
    this.showValidation = false;
  }
  handleChange(event) {
    // Validate lowercase letters
    let passwordValue = event.target.value;
    var lowerCaseLetters = /[a-z]/g;
    if (passwordValue.match(lowerCaseLetters)) {
      this.template
        .querySelector('[data-id="letter"]')
        .classList.remove("invalid");
      this.template.querySelector('[data-id="letter"]').classList.add("valid");
    } else {
      this.template
        .querySelector('[data-id="letter"]')
        .classList.remove("valid");
      this.template
        .querySelector('[data-id="letter"]')
        .classList.add("invalid");
    }

    // Validate capital letters
    var upperCaseLetters = /[A-Z]/g;
    if (passwordValue.match(upperCaseLetters)) {
      this.template
        .querySelector('[data-id="capital"]')
        .classList.remove("invalid");
      this.template.querySelector('[data-id="capital"]').classList.add("valid");
    } else {
      this.template
        .querySelector('[data-id="capital"]')
        .classList.remove("valid");
      this.template
        .querySelector('[data-id="capital"]')
        .classList.add("invalid");
    }

    // Validate numbers
    var numbers = /[0-9]/g;
    if (passwordValue.match(numbers)) {
      this.template
        .querySelector('[data-id="number"]')
        .classList.remove("invalid");
      this.template.querySelector('[data-id="number"]').classList.add("valid");
    } else {
      this.template
        .querySelector('[data-id="number"]')
        .classList.remove("valid");
      this.template
        .querySelector('[data-id="number"]')
        .classList.add("invalid");
    }

    // Validate length
    if (passwordValue.length >= 8) {
      this.template
        .querySelector('[data-id="length"]')
        .classList.remove("invalid");
      this.template.querySelector('[data-id="length"]').classList.add("valid");
    } else {
      this.template
        .querySelector('[data-id="length"]')
        .classList.remove("valid");
      this.template
        .querySelector('[data-id="length"]')
        .classList.add("invalid");
    }
  }
}


Output



Checkout complete video tutorial below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments