JQuery remembers the specific implementation of username and password _ jquery

Source: Internet
Author: User
This article mainly introduces JQuery's method of remembering the user name and password. For more information, see The Code is as follows:







JQuery Code

The Code is as follows:


$ (Document). ready (function (){
If ($. cookie ("rmbUser") = "true "){
$ ("# Ck_rmbUser"). prop ("checked", true );
$ ("# Username"). val ($. cookie ("username "));
$ ("# Password"). remove ();
$ ("# Pass"). append ("");
$ ("# Password"). val ($. cookie ("password "));
}
$ ("# LoginButton"). click (function (){
If (check ()){
Login ();
}
});
});


// Remember the username and password
Function save (){
If ($ ("# ck_rmbUser"). prop ("checked ")){
Var username = $ ("# username"). val ();
Var password = $ ("# password"). val ();
$. Cookie ("rmbUser", "true", {expires: 7}); // store a cookie with a 7-day validity period
$. Cookie ("username", username, {expires: 7 });
$. Cookie ("password", password, {expires: 7 });
} Else {
$. Cookie ("rmbUser", "false", {expire:-1 });
$. Cookie ("username", "", {expires:-1 });
$. Cookie ("password", "", {expires:-1 });
}
};

Function check (){
Var username = $ ("# username"). val ();
Var password = $ ("# password"). val ();
If (username = "" | username = "Enter the user name "){
$ ("# Tip"). text ("Enter the user name! ");
$ ("# Username"). focus ();
Return false;
}
If (password = "" | password = "enter the password "){
$ ("# Tip"). text ("enter the password! ");
$ ("# Password"). focus ();
Return false;
}
$ ("# Tip"). text ("");
Return true;
}

Function login (){
$. Ajax ({
Type: "POST ",
Url: "login! LoginValidate. action ",
Data: {userName: $ ("# username"). val (), password: $ ("# password"). val ()},
DataType: "json ",
BeforeSend: function (){
ShowOverlay ();
},
Success: function (data ){
If (data. success ){
AddCookie ("userName", $ ("# username"). val (), 0 );
Save ();
Location. href = "/index. jsp ";
} Else {
$ ("# Overlay"). hide ();
$ ("# Tip"). text ("the user name or password is incorrect. Please log on again! ");
Return false;
}

}
});
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.