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

Limit Password Length Using JavaScript

cancagtas

Source Code Security Auditor
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
204
Likes
165
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this article we will try to do Limit Password Length using JavaScript. The code can restrict the inputted user password. The trick is you need to compare the length of textbox and the possible maximum length in the if statement. To learn more about this, just follow the steps below.

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"

    >

    Limit Password Length Using JavaScript</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-5"

    >
  18. <form

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <label

    >

    Username</

    label

    >
  21. <input

    type

    =

    "text"

    id

    =

    "username"

    class

    =

    "form-control"

    /

    >
  22. </

    div

    >
  23. <div

    class

    =

    "form-group"

    >
  24. <label

    >

    Password</

    label

    >
  25. <input

    type

    =

    "password"

    class

    =

    "form-control"

    id

    =

    "password"

    onkeyup

    =

    "passwordValidate(this)"

    /

    >
  26. </

    div

    >
  27. <span

    id

    =

    "chk_length"

    class

    =

    "alert-danger"

    >

    X Password must be a 12 characters long</

    span

    >
  28. <br

    /

    ><br

    /

    >

  29. <center

    ><button

    type

    =

    "button"

    id

    =

    "login"

    onclick

    =

    "userLogin();"

    class

    =

    "btn btn-primary"

    disabled

    =

    "disabled"

    >

    Login</

    button

    ></

    center

    >
  30. </

    form

    >
  31. </

    div

    >
  32. </

    div

    >
  33. </

    body

    >
  34. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  35. </

    html

    >

Creating the Script

This code contains the script of the application. The code will dynamically limit the password length when user click the button. 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

    passwordValidate(

    input)

    {
  2. var

    chk_length =

    document.getElementById

    (

    'chk_length'

    )

    ;
  3. var

    bool_length;
  4. var

    password =

    input.value

    ;

  5. if

    (

    password.length

    >

    12

    )

    {
  6. chk_length.removeAttribute

    (

    'class'

    )

    ;
  7. chk_length.setAttribute

    (

    'class'

    ,

    'alert-success'

    )

    ;
  8. chk_length.innerHTML

    =

    "&#10004; Password must be a 12 characters long"

    ;
  9. bool_length =

    true

    ;
  10. }

    else

    {
  11. chk_length.removeAttribute

    (

    'class'

    )

    ;
  12. chk_length.setAttribute

    (

    'class'

    ,

    'alert-danger'

    )

    ;
  13. chk_length.innerHTML

    =

    "X Password must be a 12 characters long"

    ;
  14. bool_length =

    false

    ;
  15. }


  16. if

    (

    bool_length)

    {
  17. document.getElementById

    (

    'login'

    )

    .removeAttribute

    (

    'disabled'

    )

    ;
  18. }

    else

    {
  19. document.getElementById

    (

    'login'

    )

    .setAttribute

    (

    'disabled'

    ,

    'disabled'

    )

    ;
  20. }

  21. }

  22. function

    userLogin(

    )

    {
  23. var

    username=

    document.getElementById

    (

    'username'

    )

    .value

    ;
  24. var

    password=

    document.getElementById

    (

    'password'

    )

    .value

    ;

  25. if

    (

    username==

    ""

    )

    {
  26. alert(

    "Username must not be empty"

    )

    ;
  27. }

    else

    {
  28. alert(

    "You have login!"

    )

    ;
  29. window.location

    =

    'index.html'

    ;
  30. }

  31. }

There you have it we successfully created a Limit Password Length 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,341

323,350

Top