JQuery remembers the implementation of the username and password

  • 2020-03-30 02:34:34
  • OfStack

 
<div> 
<input id="username" type="text" class="txt1" value=" Please enter a user name " onclick="if(this.value==' Please enter a user name '){this.value=''; }" onfocus="if(this.value==' Please enter a user name '){this.value=''; }" /> 
<input id="password" type="text" class="txt2" value=" Please enter your password " onclick="if(this.value==' Please enter your password '){this.value='';this.type='password';}" onfocus="if(this.value==' Please enter your password '){this.value='';this.type='password';}"/> 
</div> 

JQuery Code
 
$(document).ready(function(){ 
if ($.cookie("rmbUser") == "true") { 
$("#ck_rmbUser").prop("checked", true); 
$("#username").val($.cookie("username")); 
$("#password").remove(); 
$("#pass").append("<input id='password' type='password' class='txt2'/>"); 
$("#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 7-day cookie
$.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 == " Please enter a user name "){ 
$("#tip").text(" Please enter a user name !"); 
$("#username").focus(); 
return false; 
} 
if(password == "" || password == " Please enter your password "){ 
$("#tip").text(" Please enter your 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(" Wrong username or password, please login again! "); 
return false; 
} 

} 
}); 
} 

Related articles: