First, you extract the asynchronous validation of a button click event as a separate function, you need to extract the variables in the button click event to define as global variables and add a variable editpass (to mark whether you're typing your password or a password read from a cookie).
The code is as follows |
Copy Code |
var wrongtypename,//user name error type, can be directly as an error message array subscript WRONGTYPEPWD,//user password error type wrongnamehtml = new Array ("", "Please enter user name", "User name length is too short", "username is over 12 digits", "your username or password is wrong", "timeout, please login again"), wrongpwdhtml = new Array ("", "Enter password", "password length is less than 6 digits", "", "password contains illegal characters"), Editpass=false; |
button click event Start
The code is as follows |
Copy Code |
$ (". Btn-submit"). Click (function () { Wrongtypename = 0; wrongtypepwd = 0; var uname = $ ("#uname"). Val (),//user name PWD = $ ("#passwd"). Val (),//user password Plength = Pwd.length, Nlength = Uname.length; Length if (nlength = 0) Wrongtypename = 1; if (nlength > 0 && nlength < 2) Wrongtypename = 2; if (Nlength > 20) Wrongtypename = 3; if (plength = 0) Wrongtypepwd = 1; Here is a judgment of the length of the username and password, and gets the subscript for the array of error messages. else { var patrn =/^ (w) {6,20}$/; if (Plength < 6) Wrongtypepwd = 2; if (Plength > 50) Wrongtypepwd = 3; if (Plength > 6 && plength < 20) { if (!patrn.exec (PWD)) Wrongtypepwd = 4; Here is a front-end judgment of the legality of the user's password and returns the subscript of the error array } } Inputtip (0, wrongnamehtml, wrongtypename); Inputtip (1, wrongpwdhtml, wrongtypepwd); if (wrongtypepwd = = 0 && wrongtypename = 0) {/////when user input information is completely legitimate, that is, the array subscript is all 0 begin to perform AJAX validation Alert ($.cookie ("logout")); if (Editpass) { PWD = $.MD5 (pwd); } $ ("#passwd"). Val (pwd); $ ("#login-form input"). attr (' disabled ', true); $ ('. Remember '). Unbind (' click '); Information has been submitted to the server, so all the input box buttons on the page are set to unavailable so that you can effectively avoid duplicate submissions var remb = $ (' #remember-long '). Val (); Ajaxcheck (uname, PWD, REMB); } }); |