欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Html5实现用户注册自动校验功能实例代码

程序员文章站 2023-01-11 11:28:04
这篇文章主要介绍了 Html5实现用户注册自动校验功能实例代码的相关资料,需要的朋友可以参考下... 16-05-24...

抽时间写了一个带有自动校验功能的html5用户注册demo。使用到handlebars模板技术和手机验证码校验。

以下是效果截图:

Html5实现用户注册自动校验功能实例代码

1.页面代码:usersregister.hbs

xml/html code复制内容到剪贴板
  1. <!doctype html>     
  2. <!--[if ie 8 ]> <html lang="en" class="ie8"> <![endif]-->     
  3. <!--[if ie 9 ]> <html lang="en" class="ie9"> <![endif]-->     
  4. <!--[if (gt ie 9)|!(ie)]><!-->     
  5. <html lang="en">     
  6. <!--<![endif]-->     
  7. <head>     
  8.     <meta http-equiv="content-type" content="text/html; charset=utf-8">     
  9.     <meta http-equiv="x-ua-compatible" content="ie=edge" />     
  10.     <title>用户注册</title>     
  11.     <!--[if lt ie 9]>     
  12.     <script src="/assets/scripts/html5shiv.js"></script>     
  13.     <![endif]-->     
  14.     <link href="/assets/styles/jquery.idealforms.min.css" rel="stylesheet" media="screen" />     
  15.     <style type="text/css">     
  16.         body {     
  17.             font: normal 15px/1.5 arial, helvetica, free sans, sans-serif;     
  18.             color: #222;     
  19.             overflow-y: scroll;     
  20.             padding: 60px 0 0 0;     
  21.         }     
  22.         .main {     
  23.             width: 560px;     
  24.             height: 480px;     
  25.             margin: -50px auto;     
  26.         }     
  27.         #my-form {     
  28.             width: 560px;     
  29.             height: 450px;     
  30.             margin: 0 auto;     
  31.             border: 1px solid #ccc;     
  32.             padding: 3em;     
  33.             border-radius: 3px;     
  34.             box-shadow: 0 0 2px rgba(0, 0, 0, .2);     
  35.         }     
  36.     </style>     
  37.     <script type="text/javascript" src="/assets/scripts/jquery-1.8.2.min.js"></script>     
  38.     <script type="text/javascript" src="/assets/scripts/jquery.idealforms.js"></script>     
  39. </head>     
  40. <body>     
  41. <!-- style="background-image: url(static/image/bg.jpg) -->     
  42.     <div class="main" >     
  43.         <div style="height:5px;text-align:center;font-size:25px"> 欢迎您注册!</div>     
  44.         <!-- begin form -->     
  45.         <form id="my-form" class="myform">     
  46.             <div>     
  47.                 <label>用户名:</label><input id="username" name="username" type="text" />     
  48.             </div>     
  49.             <div>     
  50.                 <!-- <label>密码:</label><input id="pass" name="password" type="password" /> -->     
  51.                 <label>密码:</label><input id="pass" name="password" type="text" />     
  52.             </div>     
  53.             <div>     
  54.                 <label>邮箱:</label><input id="email" name="email"     
  55.                                          data-ideal="required email" type="email" />     
  56.             </div>     
  57.             <div>     
  58.                 <label>电话:</label><input id="telephone" type="text" name="phone" data-ideal="phone" />     
  59.             </div>     
  60.             <div>     
  61.                 <label>供应商v码:</label><input id="vcode" type="text" name="vcode" data-ideal="vcode" />     
  62.             </div>     
  63.             <div>     
  64.                 <label>真实姓名:</label><input id="truename" type="text" name="truename" data-ideal="truename" />     
  65.             </div>     
  66.             <div>     
  67.                 <label>手机验证码:</label><input id="telcode" type="text" name="telcode" data-ideal="telcode" />     
  68.             </div>     
  69.             <div style="margin-bottom:5px;">     
  70.                 <button id="gettelcode" type="button" style="margin-left:160px; margin-right:auto;" >获取手机校验码</button>     
  71.                 <hr style="margin-top:5px; margin-bottom:5px;" />     
  72.             </div>     
  73.             <!--<div>     
  74.                 <label>性别:</label>     
  75.                 <select id="sex" name="sex">     
  76.                     <option value="男"></option>     
  77.                     <option value="女"></option>     
  78.                 </select>     
  79.             </div>     
  80.             <div>     
  81.                 <label>昵称:</label><input id="nickname" type="text" name="nickname" data-ideal="nickname" />     
  82.             </div>     
  83.             <div>     
  84.                 <label>年龄:</label><input id="age" type="text" name="age" data-ideal="age" />     
  85.             </div>-->     
  86.             <!-- <div>     
  87.                 <label>地址:</label><input type="text" name="address" data-ideal="address" />     
  88.             </div>     
  89.             <div>     
  90.                 <label>qq:</label><input type="text" name="qq" data-ideal="qq" />     
  91.             </div>     
  92.             <div>     
  93.                 <label>邮编:</label><input type="text" name="zip" data-ideal="zip" />     
  94.             </div>     
  95.             <div>     
  96.                 <label>传真:</label><input type="text" name="fax" data-ideal="fax" />     
  97.             </div>     
  98.             <div>     
  99.                 <label>身份证:</label><input type="text" name="creditid" data-ideal="creditid" />     
  100.             </div>     
  101.             <div>     
  102.                 <label>出生日期:</label><input name="date" class="datepicker"     
  103.                     data-ideal="date" type="text" placeholder="月/日/年" />     
  104.             </div>     
  105.             <div>     
  106.                 <label>上传头像:</label><input id="file" name="file" multiple     
  107.                     type="file" />     
  108.             </div>     
  109.             <div>     
  110.                 <label>个人主页:</label><input name="website" data-ideal="url"     
  111.                     type="text" />     
  112.             </div>     
  113.             <div>     
  114.                 <label>备注:</label>     
  115.                 <textarea id="comments" name="comments"></textarea>     
  116.             </div>     
  117.             -->     
  118.             <!-- <div id="languages">     
  119.                 <label>语言:</label> <label><input type="checkbox"     
  120.                     name="langs[]" value="english" />英文</label> <label><input     
  121.                     type="checkbox" name="langs[]" value="chinese" />中文</label> <label><input     
  122.                     type="checkbox" name="langs[]" value="spanish" />西班牙文</label> <label><input     
  123.                     type="checkbox" name="langs[]" value="french" />法文</label>     
  124.             </div>     
  125.             <div>     
  126.                 <label>精通几门:</label> <label><input type="radio"     
  127.                     name="radio" checked />1</label> <label><input type="radio"     
  128.                     name="radio" />2</label> <label><input type="radio" name="radio" />3</label>     
  129.                 <label><input type="radio" name="radio" />4</label>     
  130.             </div>     
  131.             <div>     
  132.                 <label>国籍:</label> <select id="states" name="states">     
  133.                     <option value="default">– 选择国籍 –</option>     
  134.                     <option value="al">阿拉伯</option>     
  135.                     <option value="ak">中国</option>     
  136.                     <option value="az">美国</option>     
  137.                     <option value="ar">法国</option>     
  138.                     <option value="ca">英国</option>     
  139.                     <option value="co">德国</option>     
  140.                     <option value="ct">西班牙</option>     
  141.                     <option value="de">俄罗斯</option>     
  142.                 </select>     
  143.             </div> -->     
  144.             <div style="margin-top:10px; margin-left:100px;margin-right:100px;">     
  145.                 <button type="button" id="submit" class="submit">提交</button>     
  146.                 <button id="reset" type="button" >重置</button>     
  147.             </div>     
  148.         </form>     
  149.         <!-- end form -->     
  150.     </div>     
  151. <script type="text/javascript">     
  152.     var options = {     
  153.         onfail : function() {     
  154.             alert($myform.getinvalid().length + ' invalid fields.')     
  155.         },     
  156.         inputs : {     
  157.             'password' : {     
  158.                 filters : 'required pass'     
  159.             },     
  160.             'username' : {     
  161.                 filters : 'required username'     
  162.             },     
  163.             'email' : {     
  164.                 filters : 'required email'     
  165.             },     
  166.             'phone' : {     
  167.                 filters : 'required phone'     
  168.             },     
  169.             'truename' : {     
  170.                 filters : 'required'     
  171.             },     
  172.             'vcode' : {     
  173.                 filters : 'required'     
  174.             },     
  175.             'telcode' : {     
  176.                 filters : 'required'     
  177.             }     
  178.             /*     
  179.             'age' : {     
  180.                 filters : 'required digits',     
  181.                 data : {     
  182.                    min : 16,     
  183.                    max : 70     
  184.                 }     
  185.             },     
  186.             'file' : {     
  187.                 filters : 'extension',     
  188.                 data : {     
  189.                     extension : [ 'jpg' ]     
  190.                 }     
  191.             },     
  192.             'comments' : {     
  193.                 filters : 'min max',     
  194.                 data : {     
  195.                     min : 50,     
  196.                     max : 200     
  197.                 }     
  198.             },     
  199.             'states' : {     
  200.                 filters : 'exclude',     
  201.                 data : {     
  202.                     exclude : [ 'default' ]     
  203.                 },     
  204.                 errors : {     
  205.                     exclude : '选择国籍.'     
  206.                 }     
  207.             },     
  208.             'langs[]' : {     
  209.                 filters : 'min max',     
  210.                 data : {     
  211.                     min : 2,     
  212.                     max : 3     
  213.                 },     
  214.                 errors : {     
  215.                     min : 'check at least <strong>2</strong> options.',     
  216.                     max : 'no more than <strong>3</strong> options allowed.'     
  217.                 }     
  218.             }     
  219.             */     
  220.         }     
  221.     };     
  222.     $('#gettelcode').click(function() {     
  223.         var telephone = document.getelementbyid("telephone").value;   //手机号码     
  224.         if (telephone == null || telephone == ""){     
  225.             alert("手机号码不能为空!");     
  226.         }     
  227.         else{     
  228.             $.ajax({     
  229.                 type : "get",     
  230.                 datatype : "json",     
  231.                 url : "../api/gettelcode?telephone="+ telephone,     
  232.                 success : function(msg) {     
  233.                 },     
  234.                 error : function(e) {     
  235.                     alert("获取手机校验码失败!" + e);     
  236.                 }     
  237.             });     
  238.         }     
  239.     });     
  240.     var $myform = $('#my-form').idealforms(options).data('idealforms');     
  241.     $('#submit').click(function() {     
  242.         var username = document.getelementbyid("username").value; //用户名     
  243.         var password = document.getelementbyid("pass").value;    //密码     
  244.         var email = document.getelementbyid("email").value;     //邮箱     
  245.         var telephone = document.getelementbyid("telephone").value;     //手机号码     
  246.         var vcode = document.getelementbyid("vcode").value;     //公司v码     
  247.         var telcode = document.getelementbyid("telcode").value;     //手机校验码     
  248.         var truename = document.getelementbyid("truename").value;     //真实姓名     
  249.         $.ajax({     
  250.             type : "get",     
  251.             url : "../api/usersregister?username="+ username +"password="+ password +"email="+ email +"telephone="+ telephone +"vcode="+ vcode +"telcode="+ telcode +"truename="+ truename,     
  252.             success : function(msg) {     
  253.                //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp     
  254.                var curwwwpath = window.document.location.href;     
  255.                //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp     
  256.                var pathname = window.document.location.pathname;     
  257.                var pos = curwwwpath.indexof(pathname);     
  258.                //获取主机地址,如: http://localhost:8083     
  259.                var localhostpaht = curwwwpath.substring(0, pos);     
  260.                //获取带"/"的项目名,如:/uimcardprj     
  261.                var projectname = pathname.substring(0, pathname.substr(1).indexof('/') + 1);     
  262.                window.location.href = projectname + "/login";     
  263.                alert("注册成功!");     
  264.             },     
  265.             error : function(e) {     
  266.                 alert("注册失败!" + e);     
  267.             }     
  268.         });     
  269.     });     
  270.     $('#reset').click(function() {     
  271.         $myform.reset().fresh().focusfirst();     
  272.     });     
  273. </script>     
  274. </body>     
  275. </html>    

2.jq输入校验:jquery.idealforms.js

该js校验初始版本来自cedric ruiz,我略有修改。

部分校验的规则如下:

required: '此处是必填的.'

number: '必须是数字.',

digits: '必须是唯一的数字.'

name: '必须至少有3个字符长,并且只能包含字母.'

username: '用户名最短5位,最长30位,请使用英文字母、数字、中文和下划线. 用户名首字符必须为字母、数字、中文,不能为全数字.中文最长21个字.'

pass: '密码的位数必须的在6-15位之间,并且至少包含一个数字,一个大写字母和一个小写字母.'

strongpass: '必须至少为8个字符长,至少包含一个大写字母和一个小写字母和一个数字或特殊字符.'

email: '必须是一个有效的email地址. <em>(例:

phone: '必须是一个有效的手机号码. <em>(例: 18723101212)</em>'

以下是整个代码文件:

xml/html code复制内容到剪贴板
  1. /*--------------------------------------------------------------------------    
  2.   jq-idealforms 2.1    
  3.   * author: cedric ruiz    
  4.   * license: gpl or mit    
  5.   * demo: http://elclanrs.github.com/jq-idealforms/    
  6.   *    
  7. --------------------------------------------------------------------------*/     
  8. ;(function ( $, window, document, undefined ) {     
  9.   'use strict';     
  10.   // global ideal forms namespace     
  11.   $.idealforms = {}     
  12.   $.idealforms.filters = {}     
  13.   $.idealforms.errors = {}     
  14.   $.idealforms.flags = {}     
  15.   $.idealforms.ajaxrequests = {}     
  16. /*--------------------------------------------------------------------------*/     
  17. /**    
  18.  * @namespace a chest for various utils    
  19.  */     
  20. var utils = {     
  21.   /**    
  22.    * get width of widest element in the collection.    
  23.    * @memberof utils    
  24.    * @param {jquery object} $elms    
  25.    * @returns {number}    
  26.    */     
  27.   getmaxwidth: function( $elms ) {     
  28.     var maxwidth = 0     
  29.     $elms.each(function() {     
  30.       var width = $(this).outerwidth()     
  31.       if ( width > maxwidth ) {     
  32.         maxwidth = width     
  33.       }     
  34.     })     
  35.     return maxwidth     
  36.   },     
  37.   /**    
  38.    * hacky way of getting less variables    
  39.    * @memberof utils    
  40.    * @param {string} name the name of the less class.    
  41.    * @param {string} prop the css property where the data is stored.    
  42.    * @returns {number, string}    
  43.    */     
  44.   getlessvar: function( name, prop ) {     
  45.     var value = $('<p class="' + name + '"></p>').hide().appendto('body').css( prop )     
  46.     $('.' + name).remove()     
  47.     return ( /^\d+/.test( value ) ? parseint( value, 10 ) : value )     
  48.   },     
  49.   /**    
  50.    * like es5 object.keys    
  51.    */     
  52.   getkeys: function( obj ) {     
  53.     var keys = []     
  54.     for(var key in obj) {     
  55.       if ( obj.hasownproperty( key ) ) {     
  56.         keys.push( key )     
  57.       }     
  58.     }     
  59.     return keys     
  60.   },     
  61.   // get lenght of an object     
  62.   getobjsize: function( obj ) {     
  63.     var size = 0, key;     
  64.     for ( key in obj ) {     
  65.       if ( obj.hasownproperty( key ) ) {     
  66.         size++;     
  67.       }     
  68.     }     
  69.     return size;     
  70.   },     
  71.   isfunction: function( obj ) {     
  72.     return typeof obj === 'function'     
  73.   },     
  74.   isregex: function( obj ) {     
  75.     return obj instanceof regexp     
  76.   },     
  77.   isstring: function( obj ) {     
  78.     return typeof obj === 'string'     
  79.   },     
  80.   getbynameorid: function( str ) {     
  81.     var $el = $('[name="'+ str +'"]').length     
  82.       ? $('[name="'+ str +'"]') // by name     
  83.       : $('#'+ str) // by id     
  84.     return $el.length     
  85.       ? $el     
  86.       : $.error('the field "'+ str + '" doesn\'t exist.')     
  87.   },     
  88.   getfieldsfromarray: function( fields ) {     
  89.     var f = []     
  90.     for ( var i = 0l = fields.length; i < l; i++ ) {     
  91.       f.push( utils.getbynameorid( fields[i] ).get(0) )     
  92.     }     
  93.     return $( f )     
  94.   },     
  95.   converttoarray: function( obj ) {     
  96.     return object.prototype.tostring.call( obj ) === '[object array]'     
  97.       ? obj : [ obj ]     
  98.   },     
  99.   /**    
  100.    * determine type of any ideal forms element    
  101.    * @param $input jquery $input object    
  102.    */     
  103.   getidealtype: function( $el ) {     
  104.     var type = $el.attr('type') || $el[0].tagname.tolowercase()     
  105.     return (     
  106.       /(text|password|email|number|search|url|tel|textarea)/.test( type ) && 'text' ||     
  107.       /file/.test( type ) && 'file' ||     
  108.       /select/.test( type ) && 'select' ||     
  109.       /(radio|checkbox)/.test( type ) && 'radiocheck' ||     
  110.       /(button|submit|reset)/.test( type ) && 'button' ||     
  111.       /h\d/.test( type ) && 'heading' ||     
  112.       /hr/.test( type ) && 'separator' ||     
  113.       /hidden/.test( type ) && 'hidden'     
  114.     )     
  115.   },     
  116.   /**    
  117.    * generates an input    
  118.    * @param name `name` attribute of the input    
  119.    * @param type `type` or `tagname` of the input    
  120.    */     
  121.   makeinput: function( name, value, type, list, placeholder ) {     
  122.     var markup, items = [], item, i, len     
  123.     function splitvalue( str ) {     
  124.       var item, value, arr     
  125.       if ( /::/.test( str ) ) {     
  126.         arr = str.split('::')     
  127.         item = arr[ 0 ]     
  128.         value = arr[ 1 ]     
  129.       } else {     
  130.         item = value = str     
  131.       }     
  132.       return { item: item, value: value }     
  133.     }     
  134.     // text & file     
  135.     if ( /^(text|password|email|number|search|url|tel|file|hidden)$/.test(type) )     
  136.       markup = '<input '+     
  137.         'type="'+ type +'" '+     
  138.         'id="'+ name +'" '+     
  139.         'name="'+ name +'" '+     
  140.         'value="'+ value +'" '+     
  141.         (placeholder && 'placeholder="'+ placeholder +'"') +     
  142.         '/>'     
  143.     // textarea     
  144.     if ( /textarea/.test( type ) ) {     
  145.       markup = '<textarea id="'+ name +'" name="'+ name +'" value="'+ value +'"></textarea>'     
  146.     }     
  147.     // select     
  148.     if ( /select/.test( type ) ) {     
  149.       items = []     
  150.       for ( i = 0len = list.length; i < len; i++ ) {     
  151.         item = splitvalue( list[ i ] ).item     
  152.         value = splitvalue( list[ i ] ).value     
  153.         items.push('<option value="'+ value +'">'+ item +'</option>')     
  154.       }     
  155.       markup =     
  156.         '<select id="'+ name +'" name="'+ name +'">'+     
  157.           items.join('') +     
  158.         '</select>'     
  159.     }     
  160.     // radiocheck     
  161.     if ( /(radio|checkbox)/.test( type ) ) {     
  162.       items = []     
  163.       for ( i = 0len = list.length; i < len; i++ ) {     
  164.         item = splitvalue( list[ i ] ).item     
  165.         value = splitvalue( list[ i ] ).value     
  166.         items.push(     
  167.           '<label>'+     
  168.             '<input type="'+ type +'" name="'+ name +'" value="'+ value +'" />'+     
  169.             item +     
  170.           '</label>'     
  171.         )     
  172.       }     
  173.       markup = items.join('')     
  174.     }     
  175.     return markup     
  176.   }     
  177. }     
  178. /**    
  179.  * custom tabs for ideal forms    
  180.  */     
  181. $.fn.idealtabs = function (container) {     
  182.   var     
  183.   // elements     
  184.   $contents = this,     
  185.   $containercontainer = container,     
  186.   $wrapper = $('<ul class="ideal-tabs-wrap"/>'),     
  187.   $tabs = (function () {     
  188.     var tabs = []     
  189.     $contents.each(function () {     
  190.       var name = $(this).attr('name')     
  191.       var html =     
  192.         '<li class="ideal-tabs-tab">'+     
  193.           '<span>' + name + '</span>'+     
  194.           '<i class="ideal-tabs-tab-counter ideal-tabs-tab-counter-zero">0</i>'+     
  195.         '</li>'     
  196.       tabs.push(html)     
  197.     })     
  198.     return $(tabs.join(''))     
  199.   }()),     
  200.   actions = {     
  201.     getcuridx: function () {     
  202.       return $tabs     
  203.         .filter('.ideal-tabs-tab-active')     
  204.         .index()     
  205.     },     
  206.     gettabidxbyname: function (name) {     
  207.       var re = new regexp(name, 'i')     
  208.       var $tab = $tabs.filter(function () {     
  209.         return re.test($(this).text())     
  210.       })     
  211.       return $tab.index()     
  212.     }     
  213.   },     
  214.   /**    
  215.    * public methods    
  216.    */     
  217.   methods = {     
  218.     /**    
  219.      * switch tab    
  220.      */     
  221.     switchtab: function (nameoridx) {     
  222.       var idx = utils.isstring(nameoridx)     
  223.         ? actions.gettabidxbyname(nameoridx)     
  224.         : nameoridx     
  225.       $tabs.removeclass('ideal-tabs-tab-active')     
  226.       $tabs.eq(idx).addclass('ideal-tabs-tab-active')     
  227.       $contents.hide().eq(idx).show()     
  228.     },     
  229.     nexttab: function () {     
  230.       var idx = actions.getcuridx() + 1     
  231.       idx > $tabs.length - 1     
  232.         ? methods.firsttab()     
  233.         : methods.switchtab(idx)     
  234.     },     
  235.     prevtab: function () {     
  236.       methods.switchtab(actions.getcuridx() - 1)     
  237.     },     
  238.     firsttab: function () {     
  239.       methods.switchtab(0)     
  240.     },     
  241.     lasttab: function () {     
  242.       methods.switchtab($tabs.length - 1)     
  243.     },     
  244.     updatecounter: function (nameoridx, text) {     
  245.       var idx = !isnan(nameoridx) ? nameoridx : actions.gettabidxbyname(name),     
  246.           $counter = $tabs.eq(idx).find('.ideal-tabs-tab-counter')     
  247.       $counter.removeclass('ideal-tabs-tab-counter-zero')     
  248.       if (!text) {     
  249.         $counter.addclass('ideal-tabs-tab-counter-zero')     
  250.       }     
  251.       $counter.html(text)     
  252.     }     
  253.   }     
  254.   // attach methods     
  255.   for (var m in methods)     
  256.     $contents[m] = methods[m]     
  257.   // init     
  258.   $tabs.first()     
  259.     .addclass('ideal-tabs-tab-active')     
  260.     .end()     
  261.     .click(function () {     
  262.       var name = $(this).text()     
  263.       $contents.switchtab(name)     
  264.     })     
  265.   // insert in dom & events     
  266.   $wrapper.append($tabs).appendto($container)     
  267.   $contents.addclass('ideal-tabs-content')     
  268.   $contents.each(function () {     
  269.     var $this = $(this), name = $(this).attr('name')     
  270.     $this.data('ideal-tabs-content-name', name)     
  271.       .removeattr('name')     
  272.   })     
  273.   $contents.hide().first().show() // start fresh     
  274.   return $contents     
  275. }     
  276. /**    
  277.  * a custom <select> menu jquery plugin    
  278.  * @example `$('select').idealselect()`    
  279.  */     
  280. $.fn.idealselect = function () {     
  281.   return this.each(function () {     
  282.     var     
  283.     $select = $(this),     
  284.     $options = $select.find('option')     
  285.     /**    
  286.      * generate markup and return elements of custom select    
  287.      * @memberof $.fn.tocustomselect    
  288.      * @returns {object} all elements of the new select replacement    
  289.      */     
  290.     var idealselect = (function () {     
  291.       var     
  292.       $wrap = $('<ul class="ideal-select '+ $select.attr('name') +'"/>'),     
  293.       $menu = $(     
  294.         '<li><span class="ideal-select-title">' +     
  295.           $options.filter(':selected').text() +     
  296.         '</span></li>'     
  297.       ),     
  298.       items = (function () {     
  299.         var items = []     
  300.         $options.each(function () {     
  301.           var $this = $(this)     
  302.           items.push('<li class="ideal-select-item">' + $this.text() + '</li>')     
  303.         })     
  304.         return items     
  305.       }())     
  306.       $menu.append('<ul class="ideal-select-sub">' + items.join('') + '</ul>')     
  307.       $wrap.append($menu)     
  308.       return {     
  309.         select: $wrap,     
  310.         title: $menu.find('.ideal-select-title'),     
  311.         sub: $menu.find('.ideal-select-sub'),     
  312.         items: $menu.find('.ideal-select-item')     
  313.       }     
  314.     }())     
  315.     /**    
  316.      * @namespace methods of custom select    
  317.      * @memberof $.fn.tocustomselect    
  318.      */     
  319.     var actions = {     
  320.       getselectedidx: function () {     
  321.         return idealselect.items     
  322.           .filter('.ideal-select-item-selected').index()     
  323.       },     
  324.       /**    
  325.        * @private    
  326.        */     
  327.       init: (function () {     
  328.         $select.css({     
  329.           position: 'absolute',     
  330.           left: '-9999px'     
  331.         })     
  332.         idealselect.sub.hide()     
  333.         idealselect.select.insertafter($select)     
  334.         idealselect.select.css(     
  335.           'min-width',     
  336.           utils.getmaxwidth(idealselect.items)     
  337.         )     
  338.         idealselect.items     
  339.           .eq($options.filter(':selected').index())     
  340.           .addclass('ideal-select-item-selected')     
  341.       }()),     
  342.       nowindowscroll: function (e) {     
  343.         if (e.which === 40 || e.which === 38 || e.which === 13) {     
  344.           e.preventdefault()     
  345.         }     
  346.       },     
  347.       // fix loosing focus when scrolling     
  348.       // and selecting item with keyboard     
  349.       focushack: function () {     
  350.         settimeout(function () {     
  351.           $select.trigger('focus')     
  352.         }, 1)     
  353.       },     
  354.       focus: function () {     
  355.         idealselect.select.addclass('ideal-select-focus')     
  356.         $(document).on('keydown.noscroll', actions.nowindowscroll)     
  357.       },     
  358.       blur: function () {     
  359.         idealselect.select     
  360.           .removeclass('ideal-select-open ideal-select-focus')     
  361.         $(document).off('.noscroll')     
  362.       },     
  363.       scrollintoview: function (dir) {     
  364.         var     
  365.         $selected = idealselect.items.filter('.ideal-select-item-selected'),     
  366.         itemheight = idealselect.items.outerheight(),     
  367.         menuheight = idealselect.sub.outerheight(),     
  368.         isinview = (function () {     
  369.           // relative position to the submenu     
  370.           var elpos = $selected.position().top + itemheight     
  371.           return dir === 'down'     
  372.             ? elpos <= menuheight     
  373.             : elpos > 0     
  374.         }())     
  375.         if (!isinview) {     
  376.           itemheight = (dir === 'down')     
  377.             ? itemheight // go down     
  378.             : -itemheight // go up     
  379.           idealselect.sub     
  380.             .scrolltop(idealselect.sub.scrolltop() + itemheight)     
  381.         }     
  382.       },     
  383.       scrolltoitem: function () {     
  384.         var idx = actions.getselectedidx(),     
  385.             height = idealselect.items.outerheight(),     
  386.             nitems = idealselect.items.length,     
  387.             allheight = height * nitems,     
  388.             curheight = height * (nitems - idx)     
  389.         idealselect.sub.scrolltop(allheight - curheight)     
  390.       },     
  391.       showmenu: function () {     
  392.         idealselect.sub.fadein('fast')     
  393.         idealselect.select.addclass('ideal-select-open')     
  394.         actions.select(actions.getselectedidx())     
  395.         actions.scrolltoitem()     
  396.       },     
  397.       hidemenu: function () {     
  398.         idealselect.sub.hide()     
  399.         idealselect.select.removeclass('ideal-select-open')     
  400.       },     
  401.       select: function (idx) {     
  402.         idealselect.items     
  403.           .removeclass('ideal-select-item-selected')     
  404.         idealselect.items     
  405.           .eq(idx).addclass('ideal-select-item-selected')     
  406.       },     
  407.       change: function (idx) {     
  408.         var text = idealselect.items.eq(idx).text()     
  409.         actions.select(idx)     
  410.         idealselect.title.text(text)     
  411.         $options.eq(idx).prop('selected', true)     
  412.         $select.trigger('change')     
  413.       },     
  414.       keydown: function (key) {     
  415.         var     
  416.         idx = actions.getselectedidx(),     
  417.         ismenu = idealselect.select.is('.ideal-select-menu'),     
  418.         isopen = idealselect.select.is('.ideal-select-open')     
  419.         /**    
  420.          * @namespace key pressed    
  421.          */     
  422.         var keys = {     
  423.           9: function () { // tab     
  424.             if (ismenu) {     
  425.               actions.blur()     
  426.               actions.hidemenu()     
  427.             }     
  428.           },     
  429.           13: function () { // enter     
  430.             if (ismenu)     
  431.               isopen     
  432.                 ? actions.hidemenu()     
  433.                 : actions.showmenu()     
  434.             actions.change(idx)     
  435.           },     
  436.           27: function () { // esc     
  437.             if (ismenu) actions.hidemenu()     
  438.           },     
  439.           40: function () { // down     
  440.             if (idx < $options.length - 1) {     
  441.               isopen     
  442.                 ? actions.select(idx + 1)     
  443.                 : actions.change(idx + 1)     
  444.             }     
  445.             actions.scrollintoview('down')     
  446.           },     
  447.           38: function () { // up     
  448.             if (idx > 0) {     
  449.               isopen     
  450.                 ? actions.select(idx - 1)     
  451.                 : actions.change(idx - 1)     
  452.             }     
  453.             actions.scrollintoview('up')     
  454.           },     
  455.           'default': function () { // letter     
  456.             var     
  457.             letter = string.fromcharcode(key),     
  458.             $matches = idealselect.items     
  459.               .filter(function () {     
  460.                 return /^\w+$/i.test( letter ) && // not allow modifier keys ( ctrl, cmd, meta, super... )     
  461.                   new regexp('^' + letter, 'i').test( $(this).text() ) // find first match     
  462.               }),     
  463.             nmatches = $matches.length,     
  464.             counter = idealselect.select.data('counter') + 1 || 0,     
  465.             curkey = idealselect.select.data('key') || key,     
  466.             newidx = $matches.eq(counter).index()     
  467.             if (!nmatches) // no matches     
  468.               return false     
  469.             // if more matches with same letter     
  470.             if (curkey === key) {     
  471.               if (counter < nmatches) {     
  472.                 idealselect.select.data('counter', counter)     
  473.               }     
  474.               else {     
  475.                 idealselect.select.data('counter', 0)     
  476.                 newidx = $matches.eq(0).index()     
  477.               }     
  478.             }     
  479.             // if new letter     
  480.             else {     
  481.               idealselect.select.data('counter', 0)     
  482.               newidx = $matches.eq(0).index()     
  483.             }     
  484.             if (isopen)     
  485.               actions.select(newidx)     
  486.             else     
  487.               actions.change(newidx)     
  488.             idealselect.select.data('key', key)     
  489.             actions.scrolltoitem()     
  490.             actions.focushack()     
  491.           }     
  492.         }     
  493.         keys[key]     
  494.           ? keys[key]()     
  495.           : keys['default']()     
  496.       }     
  497.     }     
  498.     /**    
  499.      * @namespace holds all events of custom select for "menu mode" and "list mode"    
  500.      * @memberof $.fn.tocustomselect    
  501.      */     
  502.     var events = {     
  503.       focus: actions.focus,     
  504.       'blur.menu': function () {     
  505.         actions.blur()     
  506.         actions.hidemenu()     
  507.       },     
  508.       'blur.list': function () {     
  509.         actions.blur()     
  510.       },     
  511.       keydown: function (e) {     
  512.         actions.keydown(e.which)     
  513.       },     
  514.       'clickitem.menu': function () {     
  515.         actions.change($(this).index())     
  516.         actions.hidemenu()     
  517.       },     
  518.       'clickitem.list': function () {     
  519.         actions.change($(this).index())     
  520.       },     
  521.       'clicktitle.menu': function () {     
  522.         actions.focus()     
  523.         actions.showmenu()     
  524.         $select.trigger('focus')     
  525.       },     
  526.       'hideoutside.menu': function () {     
  527.         $select.off('blur.menu')     
  528.         $(document).on('mousedown.ideal', function (evt) {     
  529.           if (!$(evt.target).closest(idealselect.select).length) {     
  530.             $(document).off('mousedown.ideal')     
  531.             $select.on('blur.menu', events['blur.menu'])     
  532.           } else {     
  533.             actions.focushack()     
  534.           }     
  535.         })     
  536.       },     
  537.       'mousedown.list': function () {     
  538.         actions.focushack()     
  539.       }     
  540.     }     
  541.     // reset events     
  542.     var disableevents = function () {     
  543.       idealselect.select.removeclass('ideal-select-menu ideal-select-list')     
  544.       $select.off('.menu .list')     
  545.       idealselect.items.off('.menu .list')     
  546.       idealselect.select.off('.menu .list')     
  547.       idealselect.title.off('.menu .list')     
  548.     }     
  549.     // menu mode     
  550.     idealselect.select.on('menu', function () {     
  551.       disableevents()     
  552.       idealselect.select.addclass('ideal-select-menu')     
  553.       actions.hidemenu()     
  554.       $select.on({     
  555.         'blur.menu': events['blur.menu'],     
  556.         'focus.menu': events.focus,     
  557.         'keydown.menu': events.keydown     
  558.       })     
  559.       idealselect.select.on('mousedown.menu', events['hideoutside.menu'])     
  560.       idealselect.items.on('click.menu', events['clickitem.menu'])     
  561.       idealselect.title.on('click.menu', events['clicktitle.menu'])     
  562.     })     
  563.     // list mode     
  564.     idealselect.select.on('list', function () {     
  565.       disableevents()     
  566.       idealselect.select.addclass('ideal-select-list')     
  567.       actions.showmenu()     
  568.       $select.on({     
  569.         'blur.list': events['blur.list'],     
  570.         'focus.list': events.focus,     
  571.         'keydown.list': events.keydown     
  572.       })     
  573.       idealselect.select.on('mousedown.list', events['mousedown.list'])     
  574.       idealselect.items.on('mousedown.list', events['clickitem.list'])     
  575.     })     
  576.     $select.keydown(function (e) {     
  577.       // prevent default keydown event     
  578.       // to avoid bugs with ideal select events     
  579.       if (e.which !== 9) e.preventdefault()     
  580.     })     
  581.     // reset     
  582.     idealselect.select.on('reset', function(){     
  583.       actions.change(0)     
  584.     })     
  585.     idealselect.select.trigger('menu') // default to "menu mode"     
  586.   })     
  587. }     
  588. /*    
  589.  * idealradiocheck: jquery plguin for checkbox and radio replacement    
  590.  * usage: $('input[type=checkbox], input[type=radio]').idealradiocheck()    
  591.  */     
  592. $.fn.idealradiocheck = function() {     
  593.   return this.each(function() {     
  594.     var $this = $(this)     
  595.     var $span = $('<span/>')     
  596.     $span.addclass( 'ideal-'+ ( $this.is(':checkbox') ? 'check' : 'radio' ) )     
  597.     $this.is(':checked') && $span.addclass('checked') // init     
  598.     $span.insertafter( $this )     
  599.     $this.parent('label').addclass('ideal-radiocheck-label')     
  600.       .attr('onclick', '') // fix clicking label in ios     
  601.     $this.css({ position: 'absolute', left: '-9999px' }) // hide by shifting left     
  602.     // events     
  603.     $this.on({     
  604.       change: function() {     
  605.         var $this = $(this)     
  606.         if ( $this.is('input[type="radio"]') ) {     
  607.           $this.parent().siblings('label').find('.ideal-radio').removeclass('checked')     
  608.         }     
  609.         $span.toggleclass( 'checked', $this.is(':checked') )     
  610.       },     
  611.       focus: function() { $span.addclass('focus') },     
  612.       blur: function() { $span.removeclass('focus') },     
  613.       click: function() { $(this).trigger('focus') }     
  614.     })     
  615.   })     
  616. }     
  617. ;(function( $ ) {     
  618.   // browser supports html5 multiple file?     
  619.   var multiplesupport = typeof $('<input/>')[0].multiple !== 'undefined',     
  620.       isie = /msie/i.test( navigator.useragent )     
  621.   $.fn.idealfile = function() {     
  622.     return this.each(function() {     
  623.       var $file = $(this).addclass('ideal-file'), // the original file input     
  624.           // label that will be used for ie hack     
  625.           $wrap = $('<div class="ideal-file-wrap">'),     
  626.           $input = $('<input type="text" class="ideal-file-filename" />'),     
  627.           // button that will be used in non-ie browsers     
  628.           $button = $('<button type="button" class="ideal-file-upload">open</button>'),     
  629.           // hack for ie     
  630.           $label = $('<label class="ideal-file-upload" for="'+ $file[0].id +'">open</label>')     
  631.       // hide by shifting to the left so we     
  632.       // can still trigger events     
  633.       $file.css({     
  634.         position: 'absolute',     
  635.         left: '-9999px'     
  636.       })     
  637.       $wrap.append( $input, ( isie ? $label : $button ) ).insertafter( $file )     
  638.       // prevent focus     
  639.       $file.attr('tabindex', -1)     
  640.       $button.attr('tabindex', -1)     
  641.       $button.click(function () {     
  642.         $file.focus().click() // open dialog     
  643.       })     
  644.       $file.change(function() {     
  645.         var files = [], filearr, filename     
  646.         // if multiple is supported then extract     
  647.         // all filenames from the file array     
  648.         if ( multiplesupport ) {     
  649.           filearr = $file[0].files     
  650.           for ( var i = 0len = filearr.length; i < len; i++ ) {     
  651.             files.push( filearr[i].name )     
  652.           }     
  653.           filename = files.join(', ')     
  654.         // if not supported then just take the value     
  655.         // and remove the path to just show the filename     
  656.         } else {     
  657.           filename = $file.val().split('\\').pop()     
  658.         }     
  659.         $input.val( filename ) // set the value     
  660.           .attr( 'title', filename ) // show filename in title tootlip     
  661.       })     
  662.       $input.on({     
  663.         focus: function () { $file.trigger('change') },     
  664.         blur: function () { $file.trigger('blur') },     
  665.         keydown: function( e ) {     
  666.           if ( e.which === 13 ) { // enter     
  667.             if ( !isie ) { $file.trigger('click') }     
  668.           } else if ( e.which === 8 || e.which === 46 ) { // backspace & del     
  669.             // on some browsers the value is read-only     
  670.             // with this trick we remove the old input and add     
  671.             // a clean clone with all the original events attached     
  672.             $file.replacewith( $file = $file.val('').clone( true ) )     
  673.             $file.trigger('change')     
  674.             $input.val('')     
  675.           } else if ( e.which === 9 ){ // tab     
  676.             return     
  677.           } else { // all other keys     
  678.             return false     
  679.           }     
  680.         }     
  681.       })     
  682.     })     
  683.   }     
  684. }( jquery ))     
  685. /**    
  686.  * @namespace errors    
  687.  * @locale en    
  688.  */     
  689. $.idealforms.errors = {     
  690.   required: '此处是必填的.',     
  691.   number: '必须是数字.',     
  692.   digits: '必须是唯一的数字.',     
  693.   name: '必须至少有3个字符长,并且只能包含字母.',     
  694.   username: '用户名最短5位,最长30位,请使用英文字母、数字、中文和下划线.用户名首字符必须为字母、数字、中文,不能为全数字.中文最长21个字.',     
  695.   pass: '密码的位数必须的在6-15位之间,并且至少包含一个数字,一个大写字母和一个小写字母.',     
  696.   strongpass: '必须至少为8个字符长,至少包含一个大写字母和一个小写字母和一个数字或特殊字符.',     
  697.   email: '必须是一个有效的email地址. <em>(例: user@gmail.com)</em>',     
  698.   phone: '必须是一个有效的手机号码. <em>(例: 18723101212)</em>',     
  699.   zip: 'must be a valid us zip code. <em>(e.g. 33245 or 33245-0003)</em>',     
  700.   url: 'must be a valid url. <em>(e.g. www.google.com)</em>',     
  701.   minchar: 'must be at least <strong>{0}</strong> characters long.',     
  702.   minoption: 'check at least <strong>{0}</strong> options.',     
  703.   maxchar: 'no more than <strong>{0}</strong> characters long.',     
  704.   maxoption: 'no more than <strong>{0}</strong> options allowed.',     
  705.   range: 'must be a number between {0} and {1}.',     
  706.   date: 'must be a valid date. <em>(e.g. {0})</em>',     
  707.   dob: 'must be a valid date of birth.',     
  708.   exclude: '"{0}" is not available.',     
  709.   excludeoption: '{0}',     
  710.   equalto: 'must be the same value as <strong>"{0}"</strong>',     
  711.   extension: 'file(s) must have a valid extension. <em>(e.g. "{0}")</em>',     
  712.   ajaxsuccess: '<strong>{0}</strong> is not available.',     
  713.   ajaxerror: 'server error...'     
  714. }     
  715. /**    
  716.  * get all default filters    
  717.  * @returns object    
  718.  */     
  719. var getfilters = function() {     
  720.   var filters = {     
  721.     required: {     
  722.       regex: /.+/,     
  723.       error: $.idealforms.errors.required     
  724.     },     
  725.     number: {     
  726.       regex: function( i, v ) { return !isnan(v) },     
  727.       error: $.idealforms.errors.number     
  728.     },     
  729.     digits: {     
  730.       regex: /^\d+$/,     
  731.       error: $.idealforms.errors.digits     
  732.     },     
  733.     name: {     
  734.       regex: /^[a-za-z]{3,}$/,     
  735.       error: $.idealforms.errors.name     
  736.     },     
  737.     username: {     
  738.       regex: /^[a-z](?=[\w.]{4,30}$)\w*\.?\w*$/i,     
  739.       error: $.idealforms.errors.username     
  740.     },     
  741.     pass: {     
  742.       regex: /(?=.*\d)(?=.*[a-z])(?=.*[a-z]).{6,}/,     
  743.       error: $.idealforms.errors.pass     
  744.     },     
  745.     strongpass: {     
  746.       regex: /(?=^.{8,}$)((?=.*\d)|(?=.*\w+))(?![.\n])(?=.*[a-z])(?=.*[a-z]).*$/,     
  747.       error: $.idealforms.errors.strongpass     
  748.     },     
  749.     email: {     
  750.       regex: /^([a-za-z0-9]*[-_.]?[a-za-z0-9]+)*@([a-za-z0-9]*[-_]?[a-za-z0-9]+)+[\\.][a-za-z]{2,3}([\\.][a-za-z]{2})?$/,     
  751.       error: $.idealforms.errors.email     
  752.     },     
  753.     phone: {     
  754.       //regex: /^((13[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$/,     
  755.       regex: /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/,     
  756.       error: $.idealforms.errors.phone     
  757.     },     
  758.     zip: {     
  759.       regex: /^\d{5}$|^\d{5}-\d{4}$/,     
  760.       error: $.idealforms.errors.zip     
  761.     },     
  762.     url: {     
  763.       regex: /^(?:(ftp|http|https):\/\/)?(?:[\w\-]+\.)+[a-z]{2,6}([\:\/?#].*)?$/i,     
  764.       error: $.idealforms.errors.url     
  765.     },     
  766.     min: {     
  767.       regex: function( input, value ) {     
  768.         var $inputinput = input.input,     
  769.             min = input.useroptions.data.min,     
  770.             isradiocheck = $input.is('[type="checkbox"], [type="radio"]')     
  771.         if ( isradiocheck ) {     
  772.           this.error = $.idealforms.errors.minoption.replace( '{0}', min )     
  773.           return $input.filter(':checked').length >= min     
  774.         }     
  775.         this.error = $.idealforms.errors.minchar.replace( '{0}', min )     
  776.         return value.length >= min     
  777.       }     
  778.     },     
  779.     max: {     
  780.       regex: function( input, value ) {     
  781.         var $inputinput = input.input,     
  782.             max = input.useroptions.data.max,     
  783.             isradiocheck = $input.is('[type="checkbox"], [type="radio"]')     
  784.         if ( isradiocheck ) {     
  785.           this.error = $.idealforms.errors.maxoption.replace( '{0}', max )     
  786.           return $input.filter(':checked').length <= max     
  787.         }     
  788.         this.error = $.idealforms.errors.maxchar.replace( '{0}', max )     
  789.         return value.length <= max     
  790.       }     
  791.     },     
  792.     range: {     
  793.       regex: function( input, value ) {     
  794.         var range = input.useroptions.data.range,     
  795.             val = +value     
  796.         this.error = $.idealforms.errors.range     
  797.           .replace( '{0}', range[0] )     
  798.           .replace( '{1}', range[1] )     
  799.         return val >= range[0] && val <= range[1]     
  800.       }     
  801.     },     
  802.     date: {     
  803.       regex: function( input, value ) {     
  804.         var     
  805.         userformat =     
  806.           input.useroptions.data && input.useroptions.data.date     
  807.             ? input.useroptions.data.date     
  808.             : 'mm/dd/yyyy', // default format     
  809.         delimiter = /[^mdy]/.exec( userformat )[0],     
  810.         theformat = userformat.split(delimiter),     
  811.         thedate = value.split(delimiter),     
  812.         isdate = function( date, format ) {     
  813.           var m, d, y     
  814.           for ( var i = 0len = format.length; i < len; i++ ) {     
  815.             if ( /m/.test( format[i]) ) m = date[i]     
  816.             if ( /d/.test( format[i]) ) d = date[i]     
  817.             if ( /y/.test( format[i]) ) y = date[i]     
  818.           }     
  819.           return (     
  820.             m > 0 && m < 13 &&     
  821.             y && y.length === 4 &&     
  822.             d > 0 && d <= ( new date( y, m, 0 ) ).getdate()     
  823.           )     
  824.         }     
  825.         this.error = $.idealforms.errors.date.replace( '{0}', userformat )     
  826.         return isdate( thedate, theformat )     
  827.       }     
  828.     },     
  829.     dob: {     
  830.       regex: function( input, value ) {     
  831.         var     
  832.         userformat =     
  833.           input.useroptions.data && input.useroptions.data.dob     
  834.             ? input.useroptions.data.dob     
  835.             : 'mm/dd/yyyy', // default format     
  836.         // simulate a date input     
  837.         dateinput = {     
  838.           input: input.input,     
  839.           useroptions: {     
  840.             data: { date: userformat }     
  841.           }     
  842.         },     
  843.         // use internal date filter to validate the date     
  844.         isdate = filters.date.regex( dateinput, value ),     
  845.         // dob     
  846.         theyear = /\d{4}/.exec( value ),     
  847.         maxyear = new date().getfullyear(), // current year     
  848.         minyear = maxyear - 100     
  849.         this.error = $.idealforms.errors.dob     
  850.         return isdate && theyear >= minyear && theyear <= maxyear     
  851.       }     
  852.     },     
  853.     exclude: {     
  854.       regex: function( input, value ) {     
  855.         var $inputinput = input.input,     
  856.             exclude = input.useroptions.data.exclude,     
  857.             isoption = $input.is('[type="checkbox"], [type="radio"], select')     
  858.         this.error = isoption     
  859.           ? $.idealforms.errors.excludeoption.replace( '{0}', value )     
  860.           : this.error = $.idealforms.errors.exclude.replace( '{0}', value )     
  861.         return $.inarray( value, exclude ) === -1     
  862.       }     
  863.     },     
  864.     equalto: {     
  865.       regex: function( input, value ) {     
  866.         var $equals = $( input.useroptions.data.equalto ),     
  867.             $inputinput = input.input,     
  868.             name = $equals.attr('name') || $equals.attr('id'),     
  869.             isvalid = $equals.parents('.ideal-field')     
  870.               .filter(function(){ return $(this).data('ideal-isvalid') === true })     
  871.               .length     
  872.         if ( !isvalid ) { return false }     
  873.         this.error = $.idealforms.errors.equalto.replace( '{0}', name )     
  874.         return $input.val() === $equals.val()     
  875.       }     
  876.     },     
  877.     extension: {     
  878.       regex: function( input, value ) {     
  879.         var files = input.input[0].files || [{ name: value }],     
  880.             extensions = input.useroptions.data.extension,     
  881.             re = new regexp( '\\.'+ extensions.join('|') +'$', 'i' ),     
  882.             valid = false     
  883.         for ( var i = 0len = files.length; i < len; i++ ) {     
  884.           valid = re.test( files[i].name );     
  885.         }     
  886.         this.error = $.idealforms.errors.extension.replace( '{0}', extensions.join('", "') )     
  887.         return valid     
  888.       }     
  889.     },     
  890.     ajax: {     
  891.       regex: function( input, value, showorhideerror ) {     
  892.         var self = this     
  893.         var $inputinput = input.input     
  894.         var useroptions = input.useroptions     
  895.         var name = $input.attr('name')     
  896.         var $field = $input.parents('.ideal-field')     
  897.         var valid = false     
  898.         var customerrors = useroptions.errors && useroptions.errors.ajax     
  899.         self.error = {}     
  900.         self.error.success = customerrors && customerrors.success     
  901.           ? customerrors.success     
  902.           : $.idealforms.errors.ajaxsuccess.replace( '{0}', value )     
  903.         self.error.fail = customerrors && customerrors.error     
  904.           ? customerrors.error     
  905.           : $.idealforms.errors.ajaxerror     
  906.         // send input name as $_post[name]     
  907.         var data = {}     
  908.         data[ name ] = $.trim( value )     
  909.         // ajax options defined by the user     
  910.         var userajaxops = input.useroptions.data.ajax     
  911.         var ajaxops = {     
  912.           type: 'post',     
  913.           datatype: 'json',     
  914.           data: data,     
  915.           success: function( resp, text, xhr ) {     
  916.           console.log(resp)     
  917.             showorhideerror( self.error.success, true )     
  918.             $input.data({     
  919.               'ideal-ajax-resp': resp,     
  920.               'ideal-ajax-error': self.error.success     
  921.             })     
  922.             $input.trigger('change') // to update counter     
  923.             $field.removeclass('ajax')     
  924.             // run custom success callback     
  925.             if( userajaxops._success ) {     
  926.               userajaxops._success( resp, text, xhr )     
  927.             }     
  928.           },     
  929.           error: function( xhr, text, error ) {     
  930.             if ( text !== 'abort' ) {     
  931.               showorhideerror( self.error.fail, false )     
  932.               $input.data( 'ideal-ajax-error', self.error.fail )     
  933.               $field.removeclass('ajax')     
  934.               // run custom error callback     
  935.               if ( userajaxops._error ) {     
  936.                 userajaxops._error( xhr, text, error )     
  937.               }     
  938.             }     
  939.           }     
  940.         }     
  941.         $.extend( ajaxops, userajaxops )     
  942.         // init     
  943.         $input.removedata('ideal-ajax-error')     
  944.         $input.removedata('ideal-ajax-resp')     
  945.         $field.addclass('ajax')     
  946.         // run request and save it to be able to abort it     
  947.         // so requests don't bubble     
  948.         $.idealforms.ajaxrequests[ name ] = $.ajax( ajaxops )     
  949.       }     
  950.     }     
  951.   }     
  952.   return filters     
  953. }     
  954. $.idealforms.flags = {     
  955.   noerror: function (i) {     
  956.     i.parent().siblings('.ideal-error').hide()     
  957.   },     
  958.   noicons: function (i) {     
  959.     i.siblings('.ideal-icon-valid, .ideal-icon-invalid').hide()     
  960.   },     
  961.   novalidicon: function (i) {     
  962.     i.siblings('.ideal-icon-valid').hide()     
  963.   },     
  964.   noinvalidicon: function (i) {     
  965.     i.siblings('.ideal-icon-invalid').hide()     
  966.   },     
  967.   noclass: function (i) {     
  968.     i.parents('.ideal-field').removeclass('valid invalid')     
  969.   },     
  970.   novalidclass: function (i) {     
  971.     i.parents('.ideal-field').removeclass('valid')     
  972.   },     
  973.   noinvalidclass: function (i) {     
  974.     i.parents('.ideal-field').removeclass('invalid')     
  975.   }     
  976. }     
  977. /*    
  978.  * ideal forms plugin    
  979.  */     
  980. var _defaults = {     
  981.   inputs: {},     
  982.   customfilters: {},     
  983.   customflags: {},     
  984.   globalflags: '',     
  985.   onsuccess: function(e) { alert('thank you...') },     
  986.   onfail: function() { alert('invalid!') },     
  987.   responsiveat: 'auto',     
  988.   disablecustom: ''     
  989. }     
  990. // constructor     
  991. var idealforms = function( element, options ) {     
  992.   var self = this     
  993.   self.$form = $( element )     
  994.   self.opts = $.extend( {}, _defaults, options )     
  995.   self.$tabs = self.$form.find('section')     
  996.   // set localized filters     
  997.   $.extend( $.idealforms.filters, getfilters() )     
  998.   self._init()     
  999. }     
  1000. // plugin     
  1001. $.fn.idealforms = function( options ) {     
  1002.   return this.each(function() {     
  1003.     if ( !$.data( this, 'idealforms' ) ) {     
  1004.       $.data( this, 'idealforms', new idealforms( this, options ) )     
  1005.     }     
  1006.   })     
  1007. }     
  1008. // get less variables     
  1009. var lessvars = {     
  1010.   fieldwidth: utils.getlessvar( 'ideal-field-width', 'width' )     
  1011. }     
  1012. /*    
  1013.  * private methods    
  1014.  */     
  1015. $.extend( idealforms.prototype, {     
  1016.   _init: function() {     
  1017.     var self = this     
  1018.     var o = self.opts     
  1019.     var formelements = self._getformelements()     
  1020.     self.$form.css( 'visibility', 'visible' )     
  1021.       .addclass('ideal-form')     
  1022.       .attr( 'novalidate', 'novalidate' ) // disable html5 validation     
  1023.     // do markup     
  1024.     formelements.inputs     
  1025.       .add( formelements.headings )     
  1026.       .add( formelements.separators )     
  1027.       .each(function(){ self._domarkup( $(this) ) })     
  1028.     // generate tabs     
  1029.     if ( self.$tabs.length ) {     
  1030.       var $tabcontainer = $('<div class="ideal-wrap ideal-tabs ideal-full-width"/>')     
  1031.       self.$form.prepend( $tabcontainer )     
  1032.       self.$tabs.idealtabs( $tabcontainer )     
  1033.     }     
  1034.     // always show datepicker below the input     
  1035.     if ( jquery.ui ) {     
  1036.       $.datepicker._checkoffset = function( a,b,c ) { return b }     
  1037.     }     
  1038.     // add inputs specified by data-ideal     
  1039.     // to the list of user inputs     
  1040.     self.$form.find('[data-ideal]').each(function() {     
  1041.       var userinput = o.inputs[ this.name ]     
  1042.       o.inputs[ this.name ] = userinput || { filters: $(this).data('ideal') }     
  1043.     })     
  1044.    // responsive     
  1045.     if ( o.responsiveat ) {     
  1046.       $(window).resize(function(){ self._responsive() })     
  1047.       self._responsive()     
  1048.     }     
  1049.     // form events     
  1050.     self.$form.on({     
  1051.       keydown: function( e ) {     
  1052.         // prevent submit when pressing enter     
  1053.         // but exclude textareas     
  1054.         if ( e.which === 13 && e.target.nodename !== 'textarea' ) {     
  1055.           e.preventdefault()     
  1056.         }     
  1057.       },     
  1058.       submit: function( e ) {     
  1059.         if ( !self.isvalid() ) {     
  1060.           e.preventdefault()     
  1061.           o.onfail()     
  1062.           self.focusfirstinvalid()     
  1063.         } else {     
  1064.           o.onsuccess( e )     
  1065.         }     
  1066.       }     
  1067.     })     
  1068.     self._adjust()     
  1069.     self._attachevents()     
  1070.     self.fresh() // start fresh     
  1071.   },     
  1072.   _getformelements: function() {     
  1073.     return {     
  1074.       inputs: this.$form.find('input, select, textarea, :button'),     
  1075.       labels: this.$form.find('div > label:first-child'),     
  1076.       text: this.$form.find('input:not([type="checkbox"], [type="radio"], [type="submit"]), textarea'),     
  1077.       select: this.$form.find('select'),     
  1078.       radiocheck: this.$form.find('input[type="radio"], input[type="checkbox"]'),     
  1079.       buttons: this.$form.find(':button'),     
  1080.       file: this.$form.find('input[type="file"]'),     
  1081.       headings: this.$form.find('h1, h2, h3, h4, h5, h6'),     
  1082.       separators: this.$form.find('hr'),     
  1083.       hidden: this.$form.find('input:hidden')     
  1084.     }     
  1085.   },     
  1086.   _getuserinputs: function() {     
  1087.     return this.$form.find('[name="'+ utils.getkeys( this.opts.inputs ).join('"], [name="') +'"]')     
  1088.   },     
  1089.   _gettab: function( nameoridx ) {     
  1090.     var self = this     
  1091.     var isnumber = !isnan( nameoridx )     
  1092.     if ( isnumber ) {     
  1093.       return self.$tabs.eq( nameoridx )     
  1094.     }     
  1095.     return self.$tabs.filter(function() {     
  1096.       var re = new regexp( nameoridx, 'i' )     
  1097.       return re.test( $(this).data('ideal-tabs-content-name') )     
  1098.     })     
  1099.   },     
  1100.   _getcurrenttabidx: function() {     
  1101.     return this.$tabs.index( this.$form.find('.ideal-tabs-content:visible') )     
  1102.   },     
  1103.   _updatetabscounter: function() {     
  1104.     var self = this     
  1105.     self.$tabs.each(function( i ) {     
  1106.       var invalid = self.getinvalidintab( i ).length     
  1107.       self.$tabs.updatecounter( i, invalid )     
  1108.     })     
  1109.   },     
  1110.   _adjust: function() {     
  1111.     var self = this     
  1112.     var o = self.opts     
  1113.     var formelements = self._getformelements()     
  1114.     var curtab = self._getcurrenttabidx()     
  1115.     // autocomplete causes some problems...     
  1116.     formelements.inputs.attr('autocomplete', 'off')     
  1117.     // show tabs to calculate dimensions     
  1118.     if ( self.$tabs.length ) { self.$tabs.show() }     
  1119.     // adjust labels     
  1120.     var labels = formelements.labels     
  1121.     labels.removeattr('style').width( utils.getmaxwidth( labels ) )     
  1122.     // adjust headings and separators     
  1123.     if ( self.$tabs.length ) {     
  1124.       this.$tabs.each(function(){     
  1125.         $( this ).find('.ideal-heading:first').addclass('first-child')     
  1126.       })     
  1127.     } else {     
  1128.       self.$form.find('.ideal-heading:first').addclass('first-child')     
  1129.     }     
  1130.     self._setdatepicker()     
  1131.     // done calculating hide tabs     
  1132.     if ( self.$tabs.length ) {     
  1133.       self.$tabs.hide()     
  1134.       self.switchtab( curtab )     
  1135.     }     
  1136.   },     
  1137.   _setdatepicker: function() {     
  1138.     var o = this.opts     
  1139.     var $datepicker = this.$form.find('input.datepicker')     
  1140.     if ( jquery.ui && $datepicker.length ) {     
  1141.       $datepicker.each(function() {     
  1142.         var userinput = o.inputs[ this.name ]     
  1143.         var data = userinput && userinput.data && userinput.data.date     
  1144.         var format = data ? data.replace( 'yyyy', 'yy' ) : 'mm/dd/yy'     
  1145.         $(this).datepicker({     
  1146.           dateformat: format,     
  1147.           beforeshow: function( input ) {     
  1148.             $( input ).addclass('open')     
  1149.           },     
  1150.           onchangemonthyear: function() {     
  1151.             // hack to fix ie9 not resizing     
  1152.             var $this = $(this)     
  1153.             var w = $this.outerwidth() // cache first!     
  1154.             settimeout(function() {     
  1155.               $this.datepicker('widget').css( 'width', w )     
  1156.             }, 1)     
  1157.           },     
  1158.           onclose: function() { $(this).removeclass('open') }     
  1159.         })     
  1160.       })     
  1161.       // adjust width     
  1162.       $datepicker.on('focus keyup', function() {     
  1163.         var t = $(this), w = t.outerwidth()     
  1164.         t.datepicker('widget').css( 'width', w )     
  1165.       })     
  1166.       $datepicker.parent().siblings('.ideal-error').addclass('hidden')     
  1167.     }     
  1168.   },     
  1169.   _domarkup: function( $element ) {     
  1170.     var o = this.opts     
  1171.     var elementtype = utils.getidealtype( $element )     
  1172.     // validation elements     
  1173.     var $field = $('<span class="ideal-field"/>')     
  1174.     var $error = $('<span class="ideal-error" />')     
  1175.     var $valid = $('<i class="ideal-icon ideal-icon-valid" />')     
  1176.     var $invalid = $('<i class="ideal-icon ideal-icon-invalid"/>')     
  1177.       .click(function(){     
  1178.         $(this).parent().find('input:first, textarea, select').focus()     
  1179.       })     
  1180.     // basic markup     
  1181.     $element.closest('div').addclass('ideal-wrap')     
  1182.       .children('label:first-child').addclass('ideal-label')     
  1183.     var idealelements = {     
  1184.       _defaultinput: function() {     
  1185.         $element.wrapall( $field ).after( $valid, $invalid )     
  1186.           .parent().after( $error )     
  1187.       },     
  1188.       text: function() { idealelements._defaultinput() },     
  1189.       radiocheck: function() {     
  1190.         // check if input is already wrapped so we don't     
  1191.         // wrap radios and checks more than once     
  1192.         var iswrapped = $element.parents('.ideal-field').length     
  1193.         if ( !iswrapped ) {     
  1194.           $element.parent().nextall().andself().wrapall( $field.addclass('ideal-radiocheck') )     
  1195.           $element.parents('.ideal-field').append( $valid, $invalid ).after( $error )     
  1196.         }     
  1197.         if ( !/radiocheck/.test( o.disablecustom ) ) {     
  1198.           $element.idealradiocheck()     
  1199.         }     
  1200.       },     
  1201.       select: function() {     
  1202.         idealelements._defaultinput()     
  1203.         if ( !/select/.test( o.disablecustom ) ) {     
  1204.           $element.idealselect()     
  1205.         }     
  1206.       },     
  1207.       file: function() {     
  1208.         idealelements._defaultinput()     
  1209.         if ( !/file/.test( o.disablecustom ) ) {     
  1210.           $element.idealfile()     
  1211.         }     
  1212.       },     
  1213.       button: function() {     
  1214.         if ( !/button/.test( o.disablecustom ) ) {     
  1215.           $element.addclass('ideal-button')     
  1216.         }     
  1217.       },     
  1218.       hidden: function() {     
  1219.         $element.closest('div').addclass('ideal-hidden')     
  1220.       },     
  1221.       heading: function() {     
  1222.         $element.closest('div').addclass('ideal-full-width')     
  1223.         $element.parent().children().wrapall('<span class="ideal-heading"/>')     
  1224.       },     
  1225.       separator: function() {     
  1226.         $element.closest('div').addclass('ideal-full-width')     
  1227.         $element.wrapall('<div class="ideal-separator"/>')     
  1228.       }     
  1229.     }     
  1230.     // generate markup for current element type     
  1231.     idealelements[ elementtype ] ? idealelements[ elementtype ]() : $.noop()     
  1232.     $error.add( $valid ).add( $invalid ).hide() // start fresh     
  1233.   },     
  1234.   /** validates an input and shows or hides error and icon    
  1235.    * @memberof actions    
  1236.    * @param {object} $input jquery object    
  1237.    * @param {string} e the javascript event    
  1238.    */     
  1239.   _validate: function( $input, e ) {     
  1240.     var self = this     
  1241.     var o = this.opts     
  1242.     var useroptions = o.inputs[ $input.attr('name') ]     
  1243.     var userfilters = useroptions.filters && useroptions.filters.split(/\s/)     
  1244.     var name = $input.attr('name')     
  1245.     var value = $input.val()     
  1246.     var ajaxrequest = $.idealforms.ajaxrequests[ name ]     
  1247.     var isradiocheck = $input.is('[type="checkbox"], [type="radio"]')     
  1248.     var inputdata = {     
  1249.       // if is radio or check validate all inputs related by name     
  1250.       input: isradiocheck ? self.$form.find('[name="' + name + '"]') : $input,     
  1251.       useroptions: useroptions     
  1252.     }     
  1253.     // validation elements     
  1254.     var $field = $input.parents('.ideal-field')     
  1255.     var $error = $field.siblings('.ideal-error')     
  1256.     var $invalid = isradiocheck     
  1257.       ? $input.parent().siblings('.ideal-icon-invalid')     
  1258.       : $input.siblings('.ideal-icon-invalid')     
  1259.     var $valid = isradiocheck     
  1260.       ? $input.parent().siblings('.ideal-icon-valid')     
  1261.       : $input.siblings('.ideal-icon-valid')     
  1262.     function reseterror() {     
  1263.       $field.removeclass('valid invalid').removedata('ideal-isvalid')     
  1264.       $error.add( $invalid ).add( $valid ).hide()     
  1265.     }     
  1266.     function showorhideerror( error, valid ) {     
  1267.       reseterror()     
  1268.       valid ? $valid.show() : $invalid.show()     
  1269.       $field.addclass( valid ? 'valid' : 'invalid' )     
  1270.       $field.data( 'ideal-isvalid', valid )     
  1271.       if ( !valid ) {     
  1272.         $error.html( error ).toggle( $field.is('.ideal-field-focus') )     
  1273.       }     
  1274.     }     
  1275.     // prevent validation when typing but not introducing any new characters     
  1276.     // this is mainly to prevent multiple ajax requests     
  1277.     var oldvalue = $input.data('ideal-value') || 0     
  1278.     $input.data( 'ideal-value', value )     
  1279.     if ( e.type === 'keyup' && value === oldvalue ) { return false }     
  1280.     // validate     
  1281.     if ( userfilters ) {     
  1282.       $.each( userfilters, function( i, filter ) {     
  1283.         var thefilter = $.idealforms.filters[ filter ]     
  1284.         var customerror = useroptions.errors && useroptions.errors[ filter ]     
  1285.         var error = ''     
  1286.         // if field is empty and not required     
  1287.         if ( !value && filter !== 'required' ) {     
  1288.           reseterror()     
  1289.           return false     
  1290.         }     
  1291.         if ( thefilter ) {     
  1292.           // abort and reset ajax if there's a request pending     
  1293.           if ( e.type === 'keyup' && ajaxrequest ) {     
  1294.             ajaxrequest.abort()     
  1295.             $field.removeclass('ajax')     
  1296.           }     
  1297.           // ajax     
  1298.           if ( filter === 'ajax' ) {     
  1299.             showorhideerror( error, false ) // set invalid till response comes back     
  1300.             $error.hide()     
  1301.             if ( e.type === 'keyup' ) {     
  1302.               thefilter.regex( inputdata, value, showorhideerror ) // runs the ajax callback     
  1303.             } else {     
  1304.               var ajaxerror = $input.data('ideal-ajax-error')     
  1305.               if ( ajaxerror ) {     
  1306.                 showorhideerror( ajaxerror, $input.data('ideal-ajax-resp') || false )     
  1307.               }     
  1308.             }     
  1309.           }     
  1310.           // all other filters     
  1311.           else {     
  1312.             var valid = utils.isregex( thefilter.regex ) && thefilter.regex.test( value ) ||     
  1313.                         utils.isfunction( thefilter.regex ) && thefilter.regex( inputdata, value )     
  1314.             error = customerror || thefilter.error // assign error after calling regex()     
  1315.             showorhideerror( error, valid )     
  1316.             if ( !valid ) { return false }     
  1317.           }     
  1318.         }     
  1319.       })     
  1320.     }     
  1321.     // reset if there are no filters     
  1322.     else {     
  1323.       reseterror()     
  1324.     }     
  1325.     // flags     
  1326.     var flags = (function(){     
  1327.       var f = useroptions.flags && useroptions.flags.split(' ') || []     
  1328.       if ( o.globalflags ) {     
  1329.         $.each( o.globalflags.split(' '), function( i,v ) { f.push(v) })     
  1330.       }     
  1331.       return f     
  1332.     }())     
  1333.     if ( flags.length ) {     
  1334.       $.each(flags, function( i,f ) {     
  1335.         var theflag = $.idealforms.flags[f]     
  1336.         if ( theflag ) { theflag( $input, e.type ) }     
  1337.       })     
  1338.     }     
  1339.     // update counter     
  1340.     if ( self.$tabs.length ) {     
  1341.       self._updatetabscounter( self._getcurrenttabidx() )     
  1342.     }     
  1343.   },     
  1344.   _attachevents: function() {     
  1345.     var self = this     
  1346.     self._getuserinputs().on('keyup change focus blur', function(e) {     
  1347.       var $this = $(this)     
  1348.       var $field = $this.parents('.ideal-field')     
  1349.       var isfile = $this.is('input[type=file]')     
  1350.       // trigger on change if type=file cuz custom file     
  1351.       // disables focus on original file input (tabindex = -1)     
  1352.       if ( e.type === 'focus' || isfile && e.type === 'change' ) {     
  1353.         $field.addclass('ideal-field-focus')     
  1354.       }     
  1355.       if ( e.type === 'blur' ) {     
  1356.         $field.removeclass('ideal-field-focus')     
  1357.       }     
  1358.       self._validate( $this, e )     
  1359.     })     
  1360.   },     
  1361.   _responsive: function() {     
  1362.     var formelements = this._getformelements()     
  1363.     var maxwidth = lessvars.fieldwidth + formelements.labels.outerwidth()     
  1364.     var $emptylabel = formelements.labels.filter(function() {     
  1365.       return $(this).html() === ' '     
  1366.     })     
  1367.     var $customselect = this.$form.find('.ideal-select')     
  1368.     this.opts.responsiveat === 'auto'     
  1369.       ? this.$form.toggleclass( 'stack', this.$form.width() < maxwidth )     
  1370.       : this.$form.toggleclass( 'stack', $(window).width() < this.opts.responsiveat )     
  1371.     var isstack = this.$form.is('.stack')     
  1372.     $emptylabel.toggle( !isstack )     
  1373.     $customselect.trigger( isstack ? 'list' : 'menu' )     
  1374.     // hide datepicker     
  1375.     var $datepicker = this.$form.find('input.hasdatepicker')     
  1376.     if ( $datepicker.length ) { $datepicker.datepicker('hide') }     
  1377.   }     
  1378. })     
  1379. /*    
  1380.  * public methods    
  1381.  */     
  1382. $.extend( idealforms.prototype, {     
  1383.   getinvalid: function() {     
  1384.     return this.$form.find('.ideal-field').filter(function() {     
  1385.       return $(this).data('ideal-isvalid') === false     
  1386.     })     
  1387.   },     
  1388.   getinvalidintab: function( nameoridx ) {     
  1389.     return this._gettab( nameoridx ).find('.ideal-field').filter(function() {     
  1390.       return $(this).data('ideal-isvalid') === false     
  1391.     })     
  1392.   },     
  1393.   isvalid: function() {     
  1394.     return !this.getinvalid().length     
  1395.   },     
  1396.   isvalidfield: function( field ) {     
  1397.     var $input = utils.getbynameorid( field )     
  1398.     return $input.parents('.ideal-field').data('ideal-isvalid') === true     
  1399.   },     
  1400.   focusfirst: function() {     
  1401.     if ( this.$tabs.length ) {     
  1402.       this.$tabs.filter(':visible')     
  1403.         .find('.ideal-field:first')     
  1404.         .find('input:first, select, textarea').focus()     
  1405.     } else {     
  1406.       this.$form.find('.ideal-field:first')     
  1407.         .find('input:first, select, textarea').focus()     
  1408.     }     
  1409.     return this     
  1410.   },     
  1411.   focusfirstinvalid: function() {     
  1412.     var $first = this.getinvalid().first().find('input:first, select, textarea')     
  1413.     var tabname = $first.parents('.ideal-tabs-content').data('ideal-tabs-content-name')     
  1414.     if ( this.$tabs.length ) {     
  1415.       this.switchtab( tabname )     
  1416.     }     
  1417.     $first.focus()     
  1418.     return this     
  1419.   },     
  1420.   switchtab: function( nameoridx ) {     
  1421.     this.$tabs.switchtab( nameoridx )     
  1422.     return this     
  1423.   },     
  1424.   nexttab: function() {     
  1425.     this.$tabs.nexttab()     
  1426.     return this     
  1427.   },     
  1428.   prevtab: function() {     
  1429.     this.$tabs.prevtab()     
  1430.     return this     
  1431.   },     
  1432.   firsttab: function() {     
  1433.     this.$tabs.firsttab()     
  1434.     return this     
  1435.   },     
  1436.   lasttab: function() {     
  1437.     this.$tabs.lasttab()     
  1438.     return this     
  1439.   },     
  1440.   fresh: function() {     
  1441.     this._getuserinputs().change().parents('.ideal-field')     
  1442.       .removeclass('valid invalid')     
  1443.     return this     
  1444.   },     
  1445.   freshfields: function( fields ) {     
  1446.     fields = utils.converttoarray( fields )     
  1447.     $.each( fields, function( i ) {     
  1448.       var $input = utils.getbynameorid( fields[ i ] )     
  1449.       $input.change().parents('.ideal-field').removeclass('valid invalid')     
  1450.     })     
  1451.     return this     
  1452.   },     
  1453.   reload: function() {     
  1454.     this._adjust()     
  1455.     this._attachevents()     
  1456.     return this     
  1457.   },     
  1458.   reset: function() {     
  1459.     var formelements = this._getformelements()     
  1460.     formelements.text.val('') // text inputs     
  1461.     formelements.radiocheck.removeattr('checked') // radio & check     
  1462.     // select and custom select     
  1463.     formelements.select.find('option').first().prop( 'selected', true )     
  1464.     this.$form.find('.ideal-select').trigger('reset')     
  1465.     if ( this.$tabs.length ) { this.firsttab() }     
  1466.     this.focusfirst().fresh()     
  1467.     return this     
  1468.   },     
  1469.   resetfields: function( fields ) {     
  1470.     fields = utils.converttoarray( fields )     
  1471.     var formelements = this._getformelements()     
  1472.     $.each( fields, function( i, v ) {     
  1473.       var $input = utils.getbynameorid( v )     
  1474.       var type = utils.getidealtype( $input )     
  1475.       if ( type === 'text' || type === 'file' ) {     
  1476.         $input.val('')     
  1477.       }     
  1478.       if ( type === 'radiocheck' ) {     
  1479.         $input.removeattr('checked') // radio & check     
  1480.       }     
  1481.       if ( type === 'select' ) {     
  1482.         $input.find('option').first().prop( 'selected', true )     
  1483.         $input.next('.ideal-select').trigger('reset')     
  1484.       }     
  1485.       $input.change()     
  1486.     })     
  1487.     this.freshfields( fields )     
  1488.     return this     
  1489.   },     
  1490.   togglefields: function( fields ) {     
  1491.     fields = utils.converttoarray( fields )     
  1492.     var self = this     
  1493.     var $fields = utils.getfieldsfromarray( fields )     
  1494.     $fields.each(function() {     
  1495.       var $this = $(this)     
  1496.       var name = $this.attr('name') || $this.attr('id')     
  1497.       var input = self.opts.inputs[ name ]     
  1498.       var filters = input && input.filters     
  1499.       var datafilters = $this.data('ideal-filters') || ''     
  1500.       $this.data( 'ideal-filters', filters )     
  1501.       $this.closest('.ideal-wrap').toggle()     
  1502.       self.setfieldoptions( name, { filters: datafilters } )     
  1503.     })     
  1504.     return this     
  1505.   },     
  1506.   setoptions: function( options ) {     
  1507.     $.extend( true, this.opts, options )     
  1508.     this.reload().fresh()     
  1509.     return this     
  1510.   },     
  1511.   setfieldoptions: function( name, options ) {     
  1512.     $.extend( true, this.opts.inputs[ name ], options )     
  1513.     this.reload().freshfields([ name ])     
  1514.     return this     
  1515.   },     
  1516.   addfields: function( fields ) {     
  1517.     fields = utils.converttoarray( fields )     
  1518.     var self = this     
  1519.     // save names of all inputs in array     
  1520.     // to use methods that take names ie. fresh()     
  1521.     var allnames = []     
  1522.     // add an input to the dom     
  1523.     function add( ops ) {     
  1524.       var name = ops.name     
  1525.       var useroptions = {     
  1526.         filters: ops.filters || '',     
  1527.         data: ops.data || {},     
  1528.         errors: ops.errors || {},     
  1529.         flags: ops.flags || ''     
  1530.       }     
  1531.       var label = ops.label || ''     
  1532.       var type = ops.type     
  1533.       var list = ops.list || []     
  1534.       var placeholder = ops.placeholder || ''     
  1535.       var value = ops.value || ''     
  1536.       var $field = $('<div>'+     
  1537.           '<label>'+ label +':</label>'+     
  1538.           utils.makeinput( name, value, type, list, placeholder ) +     
  1539.         '</div>')     
  1540.       var $input = $field.find('input, select, textarea, :button')     
  1541.       // add inputs with filters to the list     
  1542.       // of user inputs to validate     
  1543.       if ( useroptions.filters ) { self.opts.inputs[ name ] = useroptions }     
  1544.       self._domarkup( $input )     
  1545.       // insert in dom     
  1546.       if ( ops.addafter ) {     
  1547.         $field.insertafter(     
  1548.           $( utils.getbynameorid( ops.addafter ) ).parents('.ideal-wrap')     
  1549.         )     
  1550.       } else if ( ops.addbefore ) {     
  1551.         $field.insertbefore(     
  1552.           $(utils.getbynameorid( ops.addbefore ))     
  1553.           .parents('.ideal-wrap')     
  1554.         )     
  1555.       } else if ( ops.appendtotab ) {     
  1556.         $field.insertafter(     
  1557.           self._gettab( ops.appendtotab ).find('.ideal-wrap:last-child')     
  1558.         )     
  1559.       } else {     
  1560.         $field.insertafter( self.$form.find('.ideal-wrap').last() )     
  1561.       }     
  1562.       // add current field name to list of names     
  1563.       allnames.push( name )     
  1564.     }     
  1565.     // run through each input     
  1566.     $.each( fields, function( i, ops ) { add( ops ) })     
  1567.     self.reload()     
  1568.     self.freshfields( allnames )     
  1569.     self._responsive()     
  1570.     return this     
  1571.   },     
  1572.   removefields: function( fields ) {     
  1573.     fields = utils.converttoarray( fields )     
  1574.     var $fields = utils.getfieldsfromarray( fields )     
  1575.     $fields.parents('.ideal-wrap').remove()     
  1576.     this.reload()     
  1577.     return this     
  1578.   }     
  1579. })     
  1580. }( jquery, window, document ))    

以上所述是本文的全部内容希望对大家有所帮助!