最近寫了個登陸驗證的jquery插件,其中功能還不是很完善,須要進一步改進,先放出來看看先。javascript
/** * 功能描述:本模塊實現普通網站登陸驗證,之後能夠添加二維碼驗證,以及第三方賬號登陸驗證 * 關聯腳本:md5.js,jquery.cookie.js,jquery.js * */ (function(window,$,undefined){ $.extend({ login : function(options){ /*定義全局配置參數*/ var defaults = { /*用戶名id*/ _userName : 'userName', /*密碼id*/ _pwd : 'pwd', /*驗證碼id*/ _checkCode : 'checkCode', /*錯誤信息id*/ _errMsg : 'errMsg', /*登陸按鈕id*/ _btnSubmit : 'btnSubmit', /*密碼長度*/ _pwdLength : 6, /*設定token保存的cookie有效期限,默認7天*/ _expires : 7, /*是否啓用浮動提示*/ _useInputTip :false }; var settings = $.extend(defaults, options || {}); var $form = $('form').eq(0); var $user = $('#'+ settings._userName); var $password = $('#'+ settings._pwd); var $checkCode = $("#"+ settings._checkCode); var $mess_txt = $("#"+ settings._errMsg); var $mess = $mess_txt.parent(); var $login = $('#'+ settings._btnSubmit); var md5 = new MD5(); /*定義enter按鍵提交表單驗證事件*/ var init = function (){ var userName = $user.val(); var pwd = $password.val(); /*控制提示信息是否顯示*/ if($mess_txt.length > 0){ $mess.css("display", $mess_txt.html().length > 0 ? "" : "none"); } /*控件提示控制*/ inputTip(); /*給用戶名控件綁定事件*/ $user.each(function(){ /*若初始用戶名爲空,讀取cookie中的用戶名*/ if(userName.length == 0){ userName = getCookie("userName"); if(userName.length > 0){ $user.val(userName); } } if(settings._useInputTip){ if(userName.length > 0 && pwd.length == 0){ /*若用戶名不爲空,隱藏用戶名控件提示*/ $user.prev().css("display","none"); } } $(this).bind('keyup', function (evt) { var et = evt || window.event; inputFocus( et ); }); }); /*給密碼綁定事件*/ $password.bind('keyup', function (evt) { var et = evt || window.event; inputFocus(et); }); /*若存在驗證碼,則給驗證碼綁定事件*/ if($checkCode.length > 0){ $checkCode.bind('keyup', function (evt) { var et = evt || window.event; inputFocus(et); }); } /*單擊提交表單事件*/ $login.bind('click', function() { if ($.trim($user.val()).length == 0){ showMsgTip('請輸入用戶名!'); }else if ($.trim($password.val()).length < settings._pwdLength){ showMsgTip('密碼不正確!'); }else if($checkCode.length > 0 && $.trim($checkCode.val()).length == 0){ showMsgTip('請輸入驗證碼!'); }else{ /*提交表單*/ formSubmit(); } }); }; /*實現消息提示*/ var showMsgTip = function (msg){ if($.trim(msg).length > 0){ if($mess_txt.length > 0){ $mess.show(); $mess_txt.html(msg); }else{ alert(msg); } } }; /*控件提示控制*/ var inputTip = function (){ /*控件提示控制*/ $(":text, :password", $form).each(function(){ var value = this.value; /*初始化是判斷是否顯示提示,如有內容不顯示提示*/ if(settings._useInputTip){ $(this).prev().css("display", $.trim(this.value).length > 0 ? "none" : ""); } $(this).on("focus", function(){ if(settings._useInputTip){ $(this).prev().css("display", "none"); } }).on("blur", function(){ if(settings._useInputTip){ if (this.value == null || this.value.length < 1) { $(this).prev().css("display", "block"); } } }); }); }; /*控件聚焦函數*/ var inputFocus = function (et) { var keyCode = et.keyCode; if ( keyCode == 13){ if ($.trim($user.val()).length == 0){ $user.get(0).focus(); $mess.show(); showMsgTip('請輸入用戶名!'); }else if ($.trim($password.val()).length < settings._pwdLength){ $password.get(0).focus(); showMsgTip('密碼不正確!'); }else if ($checkCode.length > 0 && $.trim($checkCode.val()).length == 0){ $checkCode.get(0).focus(); showMsgTip('請輸入驗證碼!'); }else{ /*提交表單*/ formSubmit(); } } }; /*登陸表單提交*/ var formSubmit = function (){ /*加密密碼*/ $password.val(md5.md5($password.val())); $mess.hide(); /*禁用提交按鈕*/ $login.attr("disabled", "disabled"); /*提交表單*/ $form.submit(); }; /*插件初始化*/ init(); } }); /*信息寫入cookie,默認存儲7天*/ var setCookie = function setCookie(key, value){ if($(":checkbox").get(0).checked){ $.cookie(key, value, {expires: _expires}); } }; /*讀取cookie信息*/ var getCookie = function (key){ var cookieValue = $.cookie(key); return cookieValue == undefined ? "" : cookieValue; }; })(window,jQuery);
調用方法:css
/*實現登陸驗證*/ $.login();