Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement password Box input Verification by JavaScript

Shulou Source: shulou.com Published: 2022-06-03 07:55:49 10月06日 Update

Editor to share with you how JavaScript to achieve password box input verification, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Sometimes it is necessary to do simple verification on the front-end page when the user enters it, so as to reduce the pressure on the server.

For example, the input length of the field is limited:

There is a prompt for input range after the input box.

If you enter the wrong length, it becomes an error message.

If you enter the correct length, display the correct prompt message

Realization idea

1. Write out the input prompt first

2. Define the wrong and correct classes and write the corresponding style

3. Get the element object of the input box, judge the length of the attribute value by the if statement, display different prompt contents according to different results, and set different prompt class names---to switch styles.

Sample code password box input prompt div {width: 600px; margin: 100px auto;} input {outline: none;} .message {display: inline-block; font-size: 12px; color: # 999 Background: url (images/ prompt .png) no-repeat left center/16px 16px; padding-left: 20px;} .notify {background-image: url (images/ error .png); color: red;} .right {background-image: url (images/ correct .png); color: green } Please enter the 818-digit password

Var password = document.querySelector ('.inp'); var message = document.querySelector ('.message'); password.onblur = function () {if (this.value.length)

< 8 || this.value.length >

18) {message [XSS _ clean] = 'password length error, should be 8' 18 bits'; message.className = 'message wrong';} else {message [XSS _ clean] =' password length is correct'; message.className = 'message right';}}

Page effect:

The above is all the contents of the article "how to implement password box input verification in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Input hint password information length error validation difference content article style page not so code element front end pressure most field object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB MySQL NVidia Microsoft