Home Web Front-end JS Tutorial How to use JS code to remember account and password

How to use JS code to remember account and password

Mar 06, 2018 pm 04:06 PM
javascript account

This time I will show you how to remember account and password with JS code. What are the precautions for remembering account and password with JS code? The following is a practical case, let’s take a look.

Many login functions have a "remember password" function, which is actually nothing more than reading

cookie.

Reference to add Cookie

setCookie (name, value, expdays)

Get Cookie

getCookie (name)

Delete Cookie

delCookie (name)

Code description

form form

<!-- 登陆表单 --><form method="post" autocomplete="off" onsubmit="return check()">
  <!-- 用户名输入 -->
  <input type="text"  name="username" id="username" required="required" >
  <!-- 密码输入,禁用自动填充 -->
  <input type="password" autocomplete="new-password" name="password" id="password" required="required">
  <!-- 是否记住密码复选框 -->
  <input type="checkbox" id="isRmbPwd" name="isRmbPwd" checked="checked">记住密码  <!-- 提交按钮 -->
  <input type="submit" value="提交"></form>
Copy after login

Submit check function

When you click the submit button, it will Call this function

function check (){    //获取表单输入:用户名,密码,是否保存密码
    var username = document.getElementById("username").value.trim() ;    var password = document.getElementById("password").value.trim() ;    var isRmbPwd = document.getElementById("isRmbPwd").checked ;    
    //判断用户名,密码是否为空(全空格也算空)
    if ( username.length != 0 && password.length != 0 )
    {        //若复选框勾选,则添加Cookie,记录密码
        if ( isRmbPwd == true )
        {   
            setCookie ( "This is username", username, 7 ) ;
            setCookie ( username, password, 7 ) ;
        }        //否则清除Cookie
        else
        {
            delCookie ( "This is username" ) ;
            delCookie ( username ) ;
        }        return true ;
    }    //非法输入提示
    else
    {
        alert(&#39;请输入必填字段!!!&#39;)        return false ;
    }   
}
Copy after login

Document initialization function

After the document is loaded, execute this function

//将function函数赋值给onload对象window.onload = function (){    //从Cookie获取到用户名
    var username = getCookie("This is username") ;    //如果用户名为空,则给表单元素赋空值
    if ( username == "" )
    {        document.getElementById("username").value="" ;        document.getElementById("password").value="" ;        document.getElementById("isRmbPwd").checked=false ;
    }    //获取对应的密码,并把用户名,密码赋值给表单
    else
    {        var password = getCookie(username) ;    
        document.getElementById("username").value = username ;        document.getElementById("password").value = password ;        document.getElementById("isRmbPwd").checked = true ;
    }
}
Copy after login

Enter the user name and password, and uncheck the checkbox

Submit, return to the form page

Enter the username and password, uncheck the checkbox

Submit the form, return

At this time, remove the checkbox check state ,Submit

At this time, the browser will no longer save cookies

I believe you have mastered the method after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

How to use base64 encoding for html pictures instead

How to implement scrolling fonts and pictures in marquee elements Effect

The above is the detailed content of How to use JS code to remember account and password. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to have multiple accounts on Xiaohongshu? How does it have traffic? How to have multiple accounts on Xiaohongshu? How does it have traffic? Mar 24, 2024 pm 03:36 PM

As a content-rich social e-commerce platform, Xiaohongshu has attracted a large number of users. For those who want to conduct multiple businesses on Xiaohongshu, it is particularly important to have multiple accounts. So, how does Xiaohongshu have multiple accounts? How can we make these accounts have traffic? This article will analyze these issues in detail and provide you with helpful guidance. 1. How to have multiple accounts on Xiaohongshu? When registering a Xiaohongshu account, you can choose to register multiple accounts using different mobile phone numbers or email addresses in order to have multiple accounts. Real-name authentication is an effective measure to improve account security and credibility. It is recommended to conduct real-name authentication for each account, so that the account will be more authoritative and credible. 3. Improve the information: Improve the information of each account, including personal homepage, header

What should I do if I can't log in to my Google Chrome account? Google account shows unable to log in solution What should I do if I can't log in to my Google Chrome account? Google account shows unable to log in solution Mar 17, 2024 am 08:30 AM

Google Chrome is a web browsing tool used by many people. In order to make it more convenient to use, some users want to log in to their Google account first, but a prompt that the account cannot be logged in appears. Regarding this issue, many users do not know what to do. How to solve it, then today the editor will share the complete solution with you. I hope that the content of today’s software tutorial can help the majority of users. Google account shows that you cannot log in. Solution: 1. Click on a browser on the desktop and you will see something like this after opening it. 2. If a login pops up at this time, click it. If you can't see it, click the upper right corner. 3. Click Login, then enter your account number. You do not need to enter the account after @, and click Next. 4. Enter the password,

How to change the mobile phone number of Xiaohongshu account? Will there be any impact if I change my mobile phone number? How to change the mobile phone number of Xiaohongshu account? Will there be any impact if I change my mobile phone number? Mar 22, 2024 am 08:11 AM

In the digital age, Xiaohongshu has become a platform for many people to share their lives and discover good things. Over time, we may need to change the mobile phone number on the account. So, how to change Xiaohongshu’s mobile phone number? 1. How to change the mobile phone number of Xiaohongshu account? 1. Log in to your Xiaohongshu account and enter the “My” page. 2. Click &quot;...&quot; in the upper right corner and select &quot;Settings&quot;. 3. On the settings page, find the &quot;Account and Security&quot; option and click to enter. 4. On the Account and Security page, find the &quot;Mobile Phone Number&quot; option and click to enter. 5. At this time, the page will display the current mobile phone number, click &quot;Change Mobile Number&quot;. 6. Enter the new mobile phone number, receive the verification code, and fill it out. 7. Set a new password to ensure account security. 8. After completing the above steps,

How do I have two Xiaohongshu accounts? What use does having two Xiaohongshu accounts do to creators? How do I have two Xiaohongshu accounts? What use does having two Xiaohongshu accounts do to creators? Mar 21, 2024 pm 10:11 PM

As a platform that integrates community, content and e-commerce, Xiaohongshu has attracted the attention of many creators and users. Some users want to have two Xiaohongshu accounts to better manage and operate different types of content. This article will explore how to create two Xiaohongshu accounts and explore the potential benefits of this approach for creators. 1. How to have two Xiaohongshu accounts? Registering a new account is very simple. Just download the Xiaohongshu APP from the mobile app store, then fill in the relevant information according to the prompts and set the nickname, avatar and password. 2. Log in to the original account: After registering a new account, you need to log out of the Xiaohongshu APP and then log in to your original account again. 3. Switch accounts: In the Xiaohongshu APP, click &quot;My&quot; in the lower right corner, then click the avatar and select &quot;

What should I do if my Kuaishou account is activated abnormally? -How to turn off Kuaishou password-free payment? What should I do if my Kuaishou account is activated abnormally? -How to turn off Kuaishou password-free payment? Mar 18, 2024 pm 01:30 PM

What should I do if my Kuaishou account is activated abnormally? If your Kuaishou account is abnormal and needs to be activated, you can try the following steps to solve the problem: Try to log in again: First, make sure to log in to your Kuaishou account again with the accurate username and password. Verify the accuracy of the information entered. 2. Check network connection: Make sure your device is connected to the Internet properly. Sometimes, network problems may cause abnormal account login. 3. Check account status: If your account is banned or frozen by Kuaishou due to violations or other reasons, you need to contact Kuaishou's customer support department to solve the problem. They may require you to provide identity verification or other information to activate your account. 4. Verification code or mobile phone verification: Kuaishou may require you to conduct mobile phone verification or enter a verification code to ensure the security of your account.

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

In today's society, mobile phones have become an indispensable part of our lives. As an important tool for our daily communication, work, and life, WeChat is often used. However, it may be necessary to separate two WeChat accounts when handling different transactions, which requires the mobile phone to support logging in to two WeChat accounts at the same time. As a well-known domestic brand, Huawei mobile phones are used by many people. So what is the method to open two WeChat accounts on Huawei mobile phones? Let’s reveal the secret of this method. First of all, you need to use two WeChat accounts at the same time on your Huawei mobile phone. The easiest way is to

How do I switch accounts in Xiaohongshu and it remains the same? How do I get the account switching function? How do I switch accounts in Xiaohongshu and it remains the same? How do I get the account switching function? Mar 21, 2024 pm 09:06 PM

As a popular social e-commerce platform, Xiaohongshu has attracted a large number of users. Many users may encounter the problem of needing to switch accounts when registering Xiaohongshu. So, how to switch accounts on Xiaohongshu without changing the original account? This article will answer this question in detail and help you better use Xiaohongshu. 1. How to switch accounts in Xiaohongshu so that the original account remains unchanged? 1. Open the Xiaohongshu APP and enter the personal center. 2. Click the &quot;Settings&quot; button in the upper right corner to enter the setting interface. 3. In the settings interface, find the &quot;Account and Security&quot; option and click to enter. 4. In the account and security interface, find the &quot;Switch Account&quot; option and click to enter. 5. Enter the password of the original account and confirm the account switch. 6. After the switch is successful, you will return to the personal center of the original account. 2. Xiaohong

See all articles