• We just launched and are currently in beta. Join us as we build and grow the community.

JavaScript - User Password Validator

jeplowman23

Follower Insights Pro
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
110
Likes
83
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a User Password Validator using JavaScript. This code will automatically validate the password input when user enter some value in the textbox. The code itself use onkeyup() to call a function that will validate the password if it is acceptable to be use in any terms This code is free to use, you can modify it as your own,

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    JavaScript - User Password Validator</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-5"

    >
  18. <form

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <label

    >

    Enter a password</

    label

    >
  21. <input

    type

    =

    "password"

    onkeyup

    =

    "CheckPassword(this);"

    class

    =

    "form-control"

    /

    >
  22. </

    div

    >
  23. <span

    id

    =

    "chk_capital"

    class

    =

    "alert-danger"

    >

    X One capital in first letter</

    span

    >
  24. <br

    /

    >
  25. <span

    id

    =

    "chk_number"

    class

    =

    "alert-danger"

    >

    X Have a number digit</

    span

    >
  26. <br

    /

    >
  27. <br

    /

    >

  28. <center

    ><button

    type

    =

    "button"

    id

    =

    "btn_check"

    class

    =

    "btn btn-primary"

    disabled

    =

    "disabled"

    >

    Check</

    button

    ></

    center

    >

  29. </

    form

    >
  30. </

    div

    >
  31. </

    div

    >
  32. </

    body

    >
  33. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  34. </

    html

    >

Creating the Script

This code contains the script of the application. This code will automatically validate the form inputs when the inputs is entered. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function

    CheckPassword(

    password)

    {
  2. var

    chk_capital =

    document.getElementById

    (

    'chk_capital'

    )

    ;
  3. var

    chk_number =

    document.getElementById

    (

    'chk_number'

    )

    ;
  4. var

    bool_capital;
  5. var

    bool_number;

  6. if

    (

    password.value

    .search

    (

    /[0-9]/

    )

    >

    0

    )

    {
  7. chk_number.removeAttribute

    (

    'class'

    )

    ;
  8. chk_number.setAttribute

    (

    'class'

    ,

    'alert-success'

    )

    ;
  9. chk_number.innerHTML

    =

    "&#10004; Validated"

    ;
  10. bool_number =

    true

    ;
  11. }

    else

    {
  12. chk_number.removeAttribute

    (

    'class'

    )

    ;
  13. chk_number.setAttribute

    (

    'class'

    ,

    'alert-danger'

    )

    ;
  14. chk_number.innerHTML

    =

    "X Have a number digit"

    ;
  15. bool_number =

    false

    ;
  16. }

  17. if

    (

    password.value

    .match

    (

    /[A-Z]/

    )

    )

    {
  18. chk_capital.removeAttribute

    (

    'class'

    )

    ;
  19. chk_capital.setAttribute

    (

    'class'

    ,

    'alert-success'

    )

    ;
  20. chk_capital.innerHTML

    =

    "&#10004; Validated"

    ;
  21. bool_capital =

    true

    ;
  22. }

    else

    {
  23. chk_capital.removeAttribute

    (

    'class'

    )

    ;
  24. chk_capital.setAttribute

    (

    'class'

    ,

    'alert-danger'

    )

    ;
  25. chk_capital.innerHTML

    =

    "X One capital in first letter"

    ;
  26. bool_capital =

    false

    ;
  27. }

  28. if

    (

    bool_capital &&

    bool_number)

    {
  29. document.getElementById

    (

    'btn_check'

    )

    .removeAttribute

    (

    'disabled'

    )

    ;
  30. }

    else

    {
  31. document.getElementById

    (

    'btn_check'

    )

    .setAttribute

    (

    'disabled'

    ,

    'disabled'

    )

    ;
  32. }

  33. }

There you have it we successfully created a User Password Validator using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top