jquery簡易検証プラグインの実装


本論文の例では、jquery簡易認証プラグインのパッケージの具体的なコードを共有しています。
htmlコード:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>jQuery        </title>
 <link rel="stylesheet" href="css/register.css" />
 <link rel="stylesheet" href="css/validate.css" />
</head>
<body>
 <main>
 <section>
 <form method="post" action="result.html" ac id="register">
 <div class="register-wrap">
 <div class="register">
 <ul>
 <li>
 <label for="username">   :</label>
 <input type="text" id="username" name="username" tabindex="1" class="format-input" placeholder="      " data-vt-required-msg="       " data-vt-regexp-msg="         、  、     ,        (6-20 )" data-vt-required=true data-vt-regexp='^[\w_]\w{5,19}$' autofocus>
 </li>
 <li>
 <label for="pwd">  :</label>
 <input type="password" id="pwd" name="password" tabindex="2" class="format-input" placeholder="     " data-vt-required=true data-vt-regexp="^[a-zA-Z_][\w_]{5,11}$" data-vt-required-msg="      " data-vt-regexp-msg="        、  、     ,        (6-12 )" >
 </li>
 <li>
 <label for="confirmPwd">    :</label>
 <input type="password" id="confirmPwd" name="password" tabindex="3" class="format-input" placeholder="       " data-vt-required=true data-vt-required-msg="      " data-vt-equals=true data-vt-equals-msg="       ">
 </li>
 <li>
 <label for="phone">   :</label>
 <input type="text" id="phone" name="phone" tabindex="4" class="format-input" placeholder="      " data-vt-required=true data-vt-phone=true data-vt-required-msg="       " data-vt-phone-msg="      ">
 </li>
 <li>
 <label for="tel">  :</label>
 <input type="text" id="tel" name="tel" tabindex="5" class="format-input" placeholder="       " data-vt-required=true data-vt-tel=true data-vt-required-msg="       " data-vt-tel-msg="      ">
 </li>
 <li>
 <label for="email">  :</label>
 <input type="text" id="email" name="email" tabindex="6" class="format-input" placeholder="       " data-vt-required=true data-vt-email=true data-vt-required-msg="      " data-vt-email-msg="     " >
 </li>
 <li>
 <label for="submitBtn"></label>
 <input type="submit" value="  " id="submitBtn" tabindex="7" class="format-input submit-btn">
 </li>
 </ul>
 </div>
 </div>
 </form>
 </section>
 </main>
 <script src="js/jquery2.0.js"></script>
 <script src="js/formValidate.js"></script>
 <script>
 $(function(){
 $('#register').formValidate();
 });
 </script>
</body>
</html>
css部分

* {
 margin: 0;
 padding: 0;
 box-sizing: border-box;
 -webkit-box-sizing: border-box;
 -moz-box-sizing: border-box;
}
ul li {
 list-style: none;
 position: relative;
}
input {
 outline: 0;
}
.format-input {
 display: inline-block;
 width: 84%;
 height: 35px;
 padding: 0 0 0 3px;
 border: 1px solid #fff;
 vertical-align: baseline;
}
:focus {
 outline: 4px solid #007fff;
}
html,
body {
 width: 100%;
 height: 100%;
}
body {
 min-height: 100%;
 font: 12px/1.5 'Microsoft YaHei', arial, sans-serif;
 background: url(../image/bj.jpg) no-repeat;
 background-size: cover;
 overflow: hidden;
}
.register-wrap {
 position: absolute;
 top: 0;
 bottom: 0;
 left: 0;
 right: 0;
 width: 450px;
 height: 415px;
 margin: auto;
 background: rgba(0, 0, 0, 0.5);
}
.register {
 width: 100%;
 height: 100%;
 padding: 20px 30px;
 color: #fff;
}
.register > ul > li {
 font-size: 0;
 margin: 0 0 20px 0;
}
.register > ul > li label {
 display: inline-block;
 width: 16%;
 font-size: 12px;
}
.submit-btn {
 border: 1px solid transparent;
 font-size: 18px;
 font-weight: bold;
 color: #fff;
 background: #51a8ff;
 box-shadow: 1px 1px #AFC4EA,
  2px 2px #AFC4EA,
  3px 3px #AFC4EA;
}
.submit-btn:hover {
 cursor: pointer;
}
.submit-btn:focus {
 outline: none;
 border: 1px solid #f0f3f9;
}
.submit-btn:active {
 border: 1px solid #f0f3f9;
 transform: translate(1px, 1px);
 box-shadow: 1px 1px #AFC4EA,
   2px 2px #AFC4EA;
 
}
エラーのtipsパターンを提示します。

.validate-error-tip {
 position: absolute;
 top: 0;
 left: 0;
 display: table;
 min-width: 150px;
 min-height: 35px;
 font-size: 12px;
 border: 1px solid lightblue;
 padding: 5px;
 background: #fff;
 color: #666;
 z-index: 9999;
}
.validate-error-tip:before {
 position: absolute;
 top: 0;
 left: -15px;
 display: block;
 content: '';
 width: 0;
 height: 0;
 border-color: transparent lightblue transparent transparent;
 /*1、                 ,     ;2、                ,     */
 border-style: solid;
 border-width: 15px 15px 15px 0;
}
javascript部分:

(function($, factory, pluginName) {
 
 factory($, pluginName);
 
})(jQuery, function($, pluginName){
 
 //       
 var __DEFAULT__ = {
 
 //          input  
 initEvent: 'input',
 prefix: 'vt' //       
 };
 
 //          
 var __RULES__ = {
 
 //  
 regexp: function(ruleData) {
  return new RegExp(ruleData).test(this.val());
 },
 
 //   
 required: function(ruleData) {
  return $.trim(this.val()).length > 0;
 },
 
 //   
 minLength: function(ruleData) {
  return $.trim(this.val()).length > ruleData ;
 },
 
 //   
 maxLength: function(ruleData) {
  return $.trim(this.val()).length < ruleData;
 },
 
 //          
 isEquals: function(ruleData) {
  var pwd = $(':password').eq(0); //$(':password')[0]      ?
  return pwd.val() === this.val();
 },
 
 //     
 isEmail: function(ruleData) {
  return /\w+@\w+\..+/g.test(this.val());
 },
 
 //      
 isPhone: function(ruleData) {
  return /^1\d{10}$/g.test(this.val());
 },
 
 //       
 isTel: function(ruleData) {
  return /^0\d{2,3}-\d{7,8}$/g.test(this.val());
 }
 };
 
 $.fn[pluginName] = function(options) {
 
 //        
 var $this = this;
 
 if(!$this.is('form')) { return; }
 
 //this:    this jQuery    
 $this.$file = $this.find('input:not([type="button"][type="submit"])'); //       (         jquery  )    $file   
 
 $.extend($this, __DEFAULT__, options); //        ,        
 
 //   rule  。
 //                    '-',        
 // vtEmailMsg > vt-email-msg
 $this.formatRule = function(str, connector) {
 
  if(typeof str !== 'string') {
  return str;
  }
 
  //  replace、  (        )
  str = str.replace(/[A-Z]/g,function(match, index) {
  if(index === 0) { return match.toLowerCase() }
  return connector + match.toLowerCase();
  });
  return str;
 };
 
 //      
 $this.showErrorTip = function(errorMsg) {
  var $tip = $("<div class='validate-error-tip'> </div>"),
  offset = this.position(),
  elHeight = this.outerHeight(),
  elWidth = this.outerWidth();
 
  if(this.siblings('.validate-error-tip').length > 0){
  this.siblings('.validate-error-tip').eq(0).text(errorMsg).show();
  } else {
  $tip.text(errorMsg).
   css({
   top: offset.top,
   left: offset.left + elWidth + 15,
   width: $tip.width()
   });
  this.after($tip);
  $tip.show();
  }
 };
 
 //  form      input   
 $this.$file.on(this.initEvent, function(){
 
  var $input = $(this);
 
  //       
  $input.siblings('.validate-error-tip').remove();
 
  //               ,          
  $.each(__RULES__, function(key, fn) {
  var rule = '',
   errorMsg = '';
 
  //  key  is    、   is
  if(key.indexOf('is') === 0) {
   key = key.slice(2);
  }
 
  key = $this.formatRule(key, '-'); //       html      
  rule = $input.data(__DEFAULT__.prefix + '-' + key); //      
  errorMsg = $input.data(__DEFAULT__.prefix + '-' + key + '-msg'); //         
 
  //    input     ,       
  if(rule) {
 
   //          
   var isPassed = fn.call($input, rule); //      fn     this,    input jquery  
 
   if(!isPassed) {
   //   、     
   $this.showErrorTip.call($input, errorMsg);
   }
  }
  });
 });
 
 //         
 this.on('submit', function(e) {
  var isFormPassed = true;
 
  $this.$file.trigger($this.initEvent);
 
  $this.$file.each(function(index, current){
  var $current = $(current);
 
  if($current.siblings('.validate-error-tip').is(':visible')){
   isFormPassed = false;
   return false;
  }
 
  });
 
  if(!isFormPassed) {
  return isFormPassed;
  }
 });
 };
 
 //        (         __RULES__  )
 $.fn[pluginName].addRule = function(options) {
 
 $.extend(__RULES__, options);
 }
}, 'formValidate');

もっと多い内容は「jqueryフォーム検証大全書」を参考にしてください。皆さん、勉強して読んでください。
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。