Best JavaScript code snippet using wpt
jquery.validator.js
Source:jquery.validator.js
1/**2 * @file åºäº jQuery ç validator æ件3 * @author ZwB4 * @version 1.05 * @module validator6 * @require jquery7 * @method $.validator8 * @desc9 * <pre>10 * validType éªè¯ç±»å 对象éå æ¯ä¸ä¸ªå±æ§é½æ¯ä¸ç§éªè¯ç±»å« æ¯ä¸ä¸ªå±æ§å¯¹åºä¸ä¸ªå¯¹è±¡11 该对象ä¸çå±æ§å 对åºçæ§è¡éªè¯çæ¹æ³ å¼ä¸ºæ示信æ¯12 * validMethods éªè¯æ¹æ³ 对象éå æ¯ä¸ä¸ªæ¹æ³é½æ¯ä½¿ç¨ apply æ¹å¼è¢«è°ç¨ï¼æ¹æ³ä¸ç this æ¯è¿è¡éªè¯ç jQuery 对象13 * </pre>14 * @param {object} options15 * @param {(string|object)} options.selector éè¦éªè¯ç表å对象16 * @param {function(string)} options.normal é置表åæ¶çæä½17 * @param {function(string)} options.focus 表å项è·å¾ç¦ç¹æ¶çæä½18 * @param {function(string)} options.wrong 表å项æªéè¿éªè¯æ¶çæä½19 * @param {function(string)} options.right 表å项éè¿éªè¯æ¶çæä½20 * @return {object} åæ° selector æ对åºç jQuery 对象21 * @example22 * JavaScript 代ç ï¼23 $(function(){24 $.validator.validType.username = {// ç¨æ·å éªè¯é
ç½®25 type: ['required', '请填åç¨æ·å'],26 trim: true, // éªè¯æ¶å¯¹å¼è¿è¡ trim27 focus: '请è¾å
¥6~12ä½æ°åãåæ¯ãä¸å线ç»åï¼å¹¶ä»¥åæ¯å¼å¤´', // è·å¾ç¦ç¹æ¶çæä½28 valid:[29 ['regexp', /^[a-z]/i],30 ['regexp', /^[a-z0-9_]$/i],31 ['length', 6, 20],32 ['ajax', 'url', '']33 ],34 text:['åªè½åæ¯å¼å¤´', 'åªè½è¾å
¥æ°åãåæ¯ãä¸å线ç»å', 'åªè½è¾å
¥6~12ä½å符', '该ç¨æ·å已被注åï¼è¯·å°è¯å
¶ä»ç¨æ·å'],35 right:'',36 callback:function(rs){alert(rs?'éè¿éªè¯':'æªéè¿éªè¯')} // éªè¯åçåè°å½æ°37 };38 var $userNameForm = $.validator({39 selector: '#userNameForm',40 normal:function(txt){41 this.next().empty();42 },43 focus:function(txt){44 this.next().html( txt );45 },46 wrong:function(txt){47 alert(txt);48 },49 right:function(txt){50 alert('éè¿éªè¯');51 }52 });53 })54 * HTML 代ç ï¼55 <form id="userNameForm" action="">56 <input type="text" name="username" id="username" data-validator="username" /><span></span>57 </form>58 * @todo éæ59 * @todo submit äºä»¶å¯ä»¥ä¸è§¦å60 * */61;(function(factory, jqPath){62 if( typeof exports === 'object' && typeof module === 'object' ){63 factory( require(jqPath || 'jquery') );64 }65 else if( typeof define === 'function' && define.amd ){66 define([jqPath || 'jquery'], factory);67 }68 else{69 factory(jQuery);70 }71})(function($){72 'use strict';73 var methods = {74 valid: function(){75 var $items = this.find('[data-validator]')76 , i = $items.length77 , validType = $.validator.validType78 , $item79 , type80 , result = true81 ;82 while( i-- ){83 $item = $items.eq(i);84 type = $item.data('validator');85 if( typeof type === 'string' ){86 type = validType[type] || {};87 $item.data('validator', type);88 }89 result = this.triggerHandler('valid', [$item, type]) && result;90 }91 return result;92 }93 , reset: function(deep){ // é置表å94 var $items = this.find('[data-validator]')95 , i = $items.length96 , normal = this.validOpts.normal97 ;98 deep && this.get(0).reset(); // é置表å项çå¼99 while( i-- ){100 normal.apply( $items.eq(i) );101 }102 }103 , result: function(){ // è¿å表åæ°æ®104 var array = this.serializeArray()105 , data = {}106 , temp107 , i = array.length108 ;109 while( i-- ){110 temp = array[i];111 if( temp.name in data ){112 data[temp.name] += ','+ temp.value;113 }114 else{115 data[temp.name] = temp.value;116 }117 }118 return data;119 }120 }121 , log = function( msg ){122 console && console.log && console.log( msg );123 }124 ;125 /**126 * @namespace Validator127 * */128 var Validator = function( options ){129 var opts = $.extend({}, Validator.defaults, options)130 , $form = opts.selector131 , validType = Validator.validType132 , validMethods = Validator.validMethods133 , validEvent = Validator.validEvent134 , validTrigger = function(){135 var $self = $(this)136 , type = $self.data('validator')137 ;138 if( typeof type === 'string' ){139 type = validType[type] || {};140 $self.data('validator', type);141 }142 $form.triggerHandler('valid', [$self, type]);143 }144 , k145 ;146 $form = (typeof $form === 'object' && $form.jQuery) ? $form : $($form);147 $form.on({148 'submit.validator': function(e){149 var rs = $form.valid();150 if( !rs ){151 e.stopImmediatePropagation();152 }153 return rs;154 }155 , 'reset.validator': function(e){156 $form.reset();157 }158 , valid: function(e, $item, type){159 var methods = type.valid || []160 , i = 0161 , j = methods.length162 , validTemp163 , validName164 , validText165 , result = true166 , isValid = true167 , isRequired = false168 , required = validMethods.required169 , wrong = opts.wrong170 ;171 if( 'trim' in type ){172 $item.val(function(){173 return $.trim( this.value );174 });175 }176 if( 'type' in type ){ // éªè¯ç±»å177 isRequired = true;178 validTemp = type.type.slice(0);179 validName = validTemp.shift();180 validText = validTemp.pop();181 // å¤æå½åå¼æ¯å¦ä¸ºç©º182 isValid = required.call($item);183 if( validName === 'required' ){ // å¿
å¡«éªè¯ å·²ç»éªè¯184 }185 else if( validName === 'or' ){ // å¤éä¸éªè¯186 if( !isValid ){187 isValid = false;188 isRequired = !validMethods.or.apply($item, validTemp);189 }190 }191 else if( validName === 'and' ){ // å¤é¡¹åæ¶å¡«å192 isValid = isValid && validMethods.and.apply($item, validTemp);193 isRequired = false;194 }195 else{ // èªå®ä¹ï¼è§ä¸ºé填项ï¼æ ¹æ®è¿åç»ææ¯å¦ç»§ç»éªè¯196 isValid = validMethods[validName].apply($item);197 isRequired = false;198 }199 result = isValid;200 }201 else{ // æªè®¾ç½® 为éå¡«202 isValid = required.call($item);203 }204 if( isValid ){ // 继ç»éªè¯205 for(; i < j; i++){206 validTemp = methods[i].slice();207 validName = validTemp.shift();208 result = validMethods[validName].apply($item, validTemp) && result;209 if( !result ){210 wrong.call($item, type.text[i] || type.text[0]);211 break;212 }213 }214 }215 else{216 if( isRequired ){217 wrong.call($item, validText);218 result = false;219 }220 else{221 opts.normal.call($item);222 result = true;223 }224 }225 if( result ){ // éè¿éªè¯226 if( 'right' in type && isValid ){227 opts.right.call($item, type.right);228 }229 else{230 opts.normal.call($item);231 }232 }233 if( 'callback' in type ){234 type.callback.call($item, result);235 }236 return result;237 }238 }).on('focus.validator', '[data-validator]', function(){239 var $self = $(this)240 , type = $self.data('validator')241 ;242 if( typeof type === 'string' ){243 type = validType[type] || {};244 $self.data('validator', type);245 }246 if( ('focus' in type) && ('focus' in opts) ){247 opts.focus.call($self, type.focus);248 }249 });250 for( k in validEvent ) if( validEvent.hasOwnProperty(k) ){251 $form.on(validEvent[k] +'.validator', k +'[data-validator]', validTrigger);252 }253 $form.validOpts = opts;254 $.extend($form, methods);255 return $form;256 };257 Validator.defaults = {258 selector:''259 , focus:null260 , wrong:null261 , right:null262 , normal:null263 };264 /**265 * @memberof Validator266 * @namespace validType267 * @desc éªè¯ç±»åéå268 * @example269 $.validator.validType.username = {// ç¨æ·å éªè¯é
ç½®270 type: ['required', '请填åç¨æ·å'],271 trim: true, // éªè¯æ¶å¯¹å¼è¿è¡ trim272 focus: '请è¾å
¥6~12ä½æ°åãåæ¯ãä¸å线ç»åï¼å¹¶ä»¥åæ¯å¼å¤´', // è·å¾ç¦ç¹æ¶çæä½273 valid:[274 ['regexp', /^[a-z]/i],275 ['regexp', /^[a-z0-9_]$/i],276 ['length', 6, 20],277 ['ajax', 'url', '']278 ],279 text:['åªè½åæ¯å¼å¤´', 'åªè½è¾å
¥æ°åãåæ¯ãä¸å线ç»å', 'åªè½è¾å
¥6~12ä½å符', '该ç¨æ·å已被注åï¼è¯·å°è¯å
¶ä»ç¨æ·å'],280 right:'',281 callback:function(rs){alert(rs?'éè¿éªè¯':'æªéè¿éªè¯')} // éªè¯åçåè°å½æ°282 };283 * */284 Validator.validType = {};285 /**286 * @memberof Validator287 * @namespace validEvent288 * @desc éªè¯äºä»¶ç±»å289 * é»è®¤äºä»¶ç±»åï¼290 * <pre>291 selector äºä»¶292 :text blur293 :password blur294 :radio click295 :checkbox click296 select change297 textarea blur298 * </pre>299 * */300 Validator.validEvent = {301 ':text': 'blur'302 , ':password': 'blur'303 , ':radio': 'click'304 , ':checkbox': 'click'305 , 'select': 'change'306 , 'textarea': 'blur'307 };308 /**309 * @memberof Validator310 * @namespace validMethods311 * @desc éªè¯æ¹æ³éå312 * */313 Validator.validMethods = {314 /**315 * å¿
填项éªè¯316 * */317 /**318 * @memberof Validator.validMethods319 * @method required320 * @return {boolean} éªè¯ç»æ321 * @desc éªè¯æ¯å¦ä¸ºç©º322 * */323 required: function(){324 var result = false;325 if( this.is(':radio,:checkbox') ){326 result = ( this.parents('form').find('input[name="'+ this.attr('name') +'"][data-validator]:checked').length > 0 );327 }328 else{329 result = !!this.val();330 }331 return result;332 }333 /**334 * @method or335 * @memberof Validator.validMethods336 * @param {string=} selector åæ°è³å° 1 个 为 jquery éæ©å¨337 * @return {boolean} éªè¯ç»æ338 * @desc æ£æµå½åæ ç¾ä¸ç®æ æ ç¾è³å°ä¸ä¸ªä¸ä¸ºç©ºï¼åæ°è³å° 1 个 为 jquery éæ©å¨339 * */340 , or: function(selector){341 var required = Validator.validMethods.required342 , result = required.apply( this )343 , argc = arguments.length344 , i = 0345 ;346 if( argc < 1 ){347 log('åæ°è®¾ç½®é误');348 result = false;349 }350 else{351 for(; i < argc; i++){352 if( result ) break;353 result = result || required.apply( $(arguments[i]) );354 }355 }356 return result;357 }358 /**359 * @method and360 * @memberof Validator.validMethods361 * @param {string=} selector åæ°è³å° 1 个 为 jquery éæ©å¨362 * @return {boolean} éªè¯ç»æ363 * @desc æ£æµå½åæ ç¾ä¸ç®æ æ ç¾åæ¶ä¸ä¸ºç©ºï¼åæ°è³å° 1 个 为 jquery éæ©å¨364 * */365 , and: function(selector){366 var required = Validator.validMethods.required367 , result = required.apply( this )368 , argc = arguments.length369 , i = 0370 ;371 if( argc < 1 ){372 log('åæ°è®¾ç½®é误');373 result = false;374 }375 else{376 for(; i < argc; i++){377 if( !result ) break;378 result = result && required.apply( $(arguments[i]) );379 }380 }381 return result;382 }383 /**384 * ç¹æ®æ ç¾éªè¯385 * */386 /**387 * @method select388 * @memberof Validator.validMethods389 * @param {number} min æå°éæ©ä¸ªæ°390 * @param {number=} max æ大éæ©ä¸ªæ°ï¼å¯éï¼391 * @return {boolean} éªè¯ç»æ392 * @desc æ£æµè®¾ç½®äº multiple å±æ§ç select æ ç¾çå¯é个æ°ï¼è³å° 1 个åæ°ï¼åæ° 1 为æå°éæ©ä¸ªæ°ï¼åæ° 2 为æ大éæ©ä¸ªæ°ï¼å¯éï¼ï¼å
¶ä½åæ°å¿½ç¥393 * */394 , select: function(min, max){395 var result = false396 , argc397 , numSelect398 ;399 if( this.is('select') && this.attr('multiple') === 'multiple' ){400 // æ¯ select æ ç¾å¹¶ä¸ select æ ç¾è®¾ç½®äº multiple å±æ§401 argc = arguments.length;402 numSelect = this.find(':selected').length;403 if( argc === 1 ){404 result = ( numSelect >= min );405 }406 else if( argc > 1 ){407 result = ( numSelect >= min && numSelect <= max );408 }409 else{410 log('åæ°è®¾ç½®é误');411 }412 }413 else{414 // ä¸æ¯ select æ ç¾æ select æ ç¾æ²¡æ multiple å±æ§415 log('æ ç¾è®¾ç½®é误');416 }417 return result;418 }419 /**420 * @method checkbox421 * @memberof Validator.validMethods422 * @param {number} min æå°éæ©ä¸ªæ°423 * @param {number=} max æ大éæ©ä¸ªæ°ï¼å¯éï¼424 * @return {boolean} éªè¯ç»æ425 * @desc æ£æµ checkbox çå¯é个æ°ï¼è³å° 1 个åæ°ï¼åæ° 1 为æå°éæ©ä¸ªæ°ï¼åæ° 2 为æ大éæ©ä¸ªæ°ï¼å¯éï¼ï¼å
¶ä½åæ°å¿½ç¥426 * */427 , checkbox: function(min, max){428 var result = false429 , argc430 , numChecked431 ;432 if( !this.is(':checkbox') ){433 log('æ ç¾é误');434 }435 else{436 argc = arguments.length;437 numChecked = this.parents('form').find('input[name="'+ this.attr('name') +'"][data-validator]:checkbox:checked').length;438 if( argc === 1 ){439 result = ( numChecked >= min );440 }441 else if( argc > 1 ){442 result = ( numChecked >= min && numChecked <= max );443 }444 else{445 log('åæ°è®¾ç½®é误');446 }447 }448 return result;449 }450 /**451 * å符串类åéªè¯452 * */453 /**454 * @method length455 * @memberof Validator.validMethods456 * @param {number} min æå°éæ©ä¸ªæ°457 * @param {number=} max æ大éæ©ä¸ªæ°ï¼å¯éï¼458 * @return {boolean} éªè¯ç»æ459 * @desc æ£æµå符串é¿åº¦ï¼è³å° 1 个åæ°ï¼åæ° 1 为æå°é¿åº¦ï¼åæ° 2 为æ大é¿åº¦ï¼å¯éï¼ï¼å
¶ä½åæ°å¿½ç¥460 * */461 , length: function(min, max){462 var result = false463 , argc = arguments.length464 , length = this.val().length465 ;466 if( argc === 1 ){467 result = ( length >= min );468 }469 else if( argc > 1 ){470 result = ( length >= min && length <= max );471 }472 else{473 log('æªç¥å¼å¸¸');474 }475 return result;476 }477 /**478 * @method regexp479 * @memberof Validator.validMethods480 * @param {regexp=} regexp481 * @return {boolean} éªè¯ç»æ482 * @desc æ£æµæ¯å¦éæ³å符ï¼reg 为æ£å表达å¼è§å é»è®¤å¼ä¸º /^[a-z0-9_]*$/i483 * */484 , regexp: function( regexp ){485 return (regexp || /^[a-z0-9_]*$/i).test( this.val() );486 }487 /**488 * æ¯è¾ç±»åéªè¯489 * */490 /**491 * @method equal492 * @memberof Validator.validMethods493 * @param {*} value ä¸ä¸ªåºå®æ°æ®494 * @return {boolean} éªè¯ç»æ495 * @desc æ£æµä¸ç®æ value æ¯å¦ç¸çï¼åæ° value 为ä¸ä¸ªåºå®æ°æ®496 * */497 , equal: function(value){498 var result = false;499 if( value ){500 result = ( this.val() === value );501 }502 else{503 log('åæ°è®¾ç½®é误');504 }505 return result;506 }507 /**508 * @method unequal509 * @memberof Validator.validMethods510 * @param {*} value ä¸ä¸ªåºå®æ°æ®511 * @return {boolean} éªè¯ç»æ512 * @desc æ£æµä¸ç®æ value æ¯å¦ä¸ç¸çï¼åæ° value 为ä¸ä¸ªåºå®æ°æ®513 * */514 , unequal: function(value){515 var result = false;516 if( value ){517 result = ( this.val() !== value );518 }519 else{520 log('åæ°è®¾ç½®é误');521 }522 return result;523 }524 /**525 * @method lt526 * @memberof Validator.validMethods527 * @param {*} value ä¸ä¸ªåºå®æ°æ®528 * @param {boolean} equal æ¯å¦å¯ä»¥çäº value529 * @return {boolean} éªè¯ç»æ530 * @desc æ£æµæ¯å¦å°äºç®æ valueï¼åæ° value 为ä¸ä¸ªåºå®æ°æ®ï¼equal æ¯å¦å¯ä»¥çäº value531 * */532 , lt: function(value, equal){533 var result = false;534 if( value ){535 result = equal ? ( this.val() <= value ) : ( this.val() < value );536 }537 else{538 log('åæ°è®¾ç½®é误');539 }540 return result;541 }542 /**543 * @method gt544 * @memberof Validator.validMethods545 * @param {*} value ä¸ä¸ªåºå®æ°æ®546 * @param {boolean} equal æ¯å¦å¯ä»¥çäº value547 * @return {boolean} éªè¯ç»æ548 * @desc æ£æµæ¯å¦å¤§äºç®æ valueï¼åæ° value 为ä¸ä¸ªåºå®æ°æ®ï¼equal æ¯å¦å¯ä»¥çäº value549 * */550 , gt: function(value, equal){551 var result = false;552 if( value ){553 result = equal ? ( this.val() >= value ) : ( this.val() > value );554 }555 else{556 log('åæ°è®¾ç½®é误');557 }558 return result;559 }560 /**561 * DOM 对象å±æ§å¼æ¯è¾ç±»åéªè¯562 * */563 /**564 * @method equalDOMProp565 * @memberof Validator.validMethods566 * @param {string} selector 为 jquery éæ©å¨ï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼567 * @param {string=} method 为 jquery æ¹æ³åï¼attr,data,html,prop,text,valï¼568 * @param {string=} name 为å±æ§å569 * @return {boolean} éªè¯ç»æ570 * @desc æ£æµä¸ç®æ dom 对象çå±æ§å¼æ¯å¦ç¸çï¼è³å°ä¸ä¸ªåæ°ï¼åæ° selector 为 jquery éæ©å¨ï¼method 为 jquery æ¹æ³åï¼attr,data,prop,text,htmlï¼ï¼name 为å±æ§åï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼571 * */572 , equalDOMProp: function(selector, method, name){573 var argc = arguments.length574 , result = false575 ;576 if( argc === 1 ){577 result = ( this.val() === $(selector).val() );578 }579 else if( argc > 1 ){580 switch( method ){581 case 'html':582 case 'text':583 case 'val':584 result = ( this.val() === $(selector)[method]() );585 break;586 case 'attr':587 case 'data':588 case 'prop':589 if( name ){590 result = ( this.val() === $(selector)[method]( name ) );591 }592 else{593 log('åæ°è®¾ç½®é误');594 }595 break;596 default:597 log('åæ°è®¾ç½®é误');598 break;599 }600 }601 else{602 log('åæ°è®¾ç½®é误');603 }604 return result;605 }606 /**607 * @method unequalDOMProp608 * @memberof Validator.validMethods609 * @param {string} selector 为 jquery éæ©å¨ï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼610 * @param {string=} method 为 jquery æ¹æ³åï¼attr,data,html,prop,text,valï¼611 * @param {string=} name 为å±æ§å612 * @return {boolean} éªè¯ç»æ613 * @desc æ£æµä¸ç®æ dom 对象çå±æ§å¼æ¯å¦ä¸ç¸çï¼è³å°ä¸ä¸ªåæ°ï¼åæ° selector 为 jquery éæ©å¨ï¼method 为 jquery æ¹æ³åï¼attr,data,prop,text,htmlï¼ï¼name 为å±æ§åï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼614 * */615 , unequalDOMProp: function(selector, method, name){616 // æ£æµä¸ç®æ dom 对象çå±æ§å¼æ¯å¦ä¸ç¸çï¼è³å°ä¸ä¸ªåæ°ï¼åæ° selector 为 jquery éæ©å¨ï¼method 为 jquery æ¹æ³åï¼attr,data,prop,text,htmlï¼ï¼name 为å±æ§åï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼617 var argc = arguments.length618 , result = false619 ;620 if( argc === 1 ){621 result = ( this.val() !== $(selector).val() );622 }623 else if( argc > 1 ){624 switch( method ){625 case 'html':626 case 'text':627 case 'val':628 result = ( this.val() !== $(selector)[method]() );629 break;630 case 'attr':631 case 'data':632 case 'prop':633 if( name ){634 result = ( this.val() !== $(selector)[method]( name ) );635 }636 else{637 log('åæ°è®¾ç½®é误');638 }639 break;640 default:641 log('åæ°è®¾ç½®é误');642 break;643 }644 }645 else{646 log('åæ°è®¾ç½®é误');647 }648 return result;649 }650 /**651 * @method ltDOMProp652 * @memberof Validator.validMethods653 * @param {string} selector 为 jquery éæ©å¨ï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼654 * @param {boolean} equal æ¯å¦å¯ä»¥ä¸ç®æ å¼ç¸ç655 * @param {string=} method 为 jquery æ¹æ³åï¼attr,data,html,prop,text,valï¼656 * @param {string=} name 为å±æ§å657 * @return {boolean} éªè¯ç»æ658 * @desc æ£æµæ¯å¦å°äºçäºç®æ dom 对象çå±æ§å¼ï¼è³å°ä¸ä¸ªåæ°ï¼åæ° selector 为 jquery éæ©å¨ï¼method 为 jquery æ¹æ³åï¼attr,data,prop,text,htmlï¼ï¼name 为å±æ§åï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼659 * */660 , ltDOMProp: function(selector, equal, method, name){661 var argc = arguments.length662 , result = false663 ;664 if( argc === 1 ){665 result = ( this.val() < $(selector).val() );666 }667 else if( argc > 1 ){668 switch( method ){669 case 'html':670 case 'text':671 case 'val':672 result = ( this.val() < $(selector)[method]() );673 break;674 case 'attr':675 case 'data':676 case 'prop':677 if( name ){678 result = ( this.val() < $(selector)[method]( name ) );679 }680 else{681 log('åæ°è®¾ç½®é误');682 }683 break;684 default:685 log('åæ°è®¾ç½®é误');686 break;687 }688 }689 else{690 log('åæ°è®¾ç½®é误');691 }692 return result;693 }694 /**695 * @method gtDOMProp696 * @memberof Validator.validMethods697 * @param {string} selector 为 jquery éæ©å¨ï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼698 * @param {boolean} equal æ¯å¦å¯ä»¥ä¸ç®æ å¼ç¸ç699 * @param {string=} method 为 jquery æ¹æ³åï¼attr,data,html,prop,text,valï¼700 * @param {string=} name 为å±æ§å701 * @return {boolean} éªè¯ç»æ702 * @desc æ£æµæ¯å¦å¤§äºçäºç®æ dom 对象çå±æ§å¼ï¼è³å°ä¸ä¸ªåæ°ï¼åæ° selector 为 jquery éæ©å¨ï¼method 为 jquery æ¹æ³åï¼attr,data,prop,text,htmlï¼ï¼name 为å±æ§åï¼è¥åªæä¸ä¸ªåæ°åé»è®¤ä» val åå¼703 * */704 , gtDOMProp: function(selector, equal, method, name){705 var argc = arguments.length706 , result = false707 ;708 if( argc === 1 ){709 result = ( this.val() > $(selector).val() );710 }711 else if( argc === 2 ){712 result = ( this.val() >= $(selector).val() );713 }714 else if( argc > 2 ){715 switch( method ){716 case 'html':717 case 'text':718 case 'val':719 result = equal ? ( this.val() >= $(selector)[method]() ) : ( this.val() > $(selector)[method]() );720 break;721 case 'attr':722 case 'data':723 case 'prop':724 if( name ){725 result = equal ? ( this.val() >= $(selector)[method]( name ) ) : ( this.val() > $(selector)[method]( name ) );726 }727 else{728 log('åæ°è®¾ç½®é误');729 }730 break;731 default:732 log('åæ°è®¾ç½®é误');733 break;734 }735 }736 else{737 log('åæ°è®¾ç½®é误');738 }739 return result;740 }741 /**742 * æ°åç±»åéªè¯743 * */744 /**745 * @method isNumber746 * @memberof Validator.validMethods747 * @return {boolean} æ£æµç»æ748 * @desc æ£æµæ¯å¦ä¸ºæ°å å
æ¬è´æ°åå°æ°çæ
åµ749 * */750 , isNumber: function(){751 return /^-?\d+\.?\d*$/.test( this.val() );752 }753 /**754 * @method limit755 * @memberof Validator.validMethods756 * @param {number} min æå°å¼757 * @param {number=} max æ大å¼ï¼å¯éï¼758 * @return {boolean} éªè¯ç»æ759 * @desc æ£æµæ°å¼å¤§å°ï¼è³å° 1 个åæ°ï¼åæ° 1 为æå°å¼ï¼åæ° 2 为æ大å¼ï¼å¯éï¼ï¼å
¶ä½åæ°å¿½ç¥760 * */761 , limit: function(min, max){762 var result = false763 , argc = arguments.length764 , value = Number( this.val() )765 ;766 if(argc === 1){767 result = ( value >= min );768 }769 else if(argc > 1){770 result = ( value >= min && value <= max );771 }772 else {773 log('åæ°è®¾ç½®é误');774 }775 return result;776 }777 /**778 * @method decimal779 * @memberof Validator.validMethods780 * @param {number} digit å°æ°ç¹å³ä¾§çæ大ä½æ°781 * @param {number=} size æ°åçæ大ä½æ°ï¼å¯éï¼782 * @return {boolean} éªè¯ç»æ783 * @desc æ£æµæµ®ç¹åæ°å¼ï¼è³å° 1 个åæ°ï¼åæ° 1 为å°æ°ç¹å³ä¾§çæ大ä½æ°ï¼åæ° 2 为æ°åçæ大ä½æ°ï¼å¯éï¼ï¼å
¶ä½åæ°å¿½ç¥784 * */785 , decimal: function(digit, size){786 var result = false787 , argc = arguments.length788 , regexp789 , temp790 ;791 if( argc === 1 ){792 regexp = new RegExp('^-?\\d+\\.?\\d{0,'+ digit +'}$');793 result = regexp.test( this.val() );794 }795 else if( argc > 1 ){796 temp = size - digit;797 regexp = new RegExp('^-?\\d'+ (temp >= 1 ? '{1'+ (temp === 1 ? '' : ','+ temp) +'}' : '+') +'\\.?\\d{0,'+ digit +'}$');798 result = regexp.test( this.val() );799 }800 else{801 log('åæ°è®¾ç½®é误');802 }803 return result;804 }805 /**806 * @method exponent807 * @memberof Validator.validMethods808 * @return {boolean} éªè¯ç»æ809 * @desc æ£æµææ°æ°å¼810 * */811 , exponent: function(){812 return /^-?\d+(?:\.\d*)?(?:e[+\-]?\d+)?$/i.test( this.val() );813 }814 /**815 * ajax éªè¯816 * todo æ¹æ å¼æ¥817 * */818 /**819 * @method ajax820 * @memberof Validator.validMethods821 * @param {string} url æå¡å¨é¾æ¥è·¯å¾822 * @param {*} expected 对æå¡å¨è¿åç»æçææå¼823 * @param {string} type åé ajax çç±»å824 * @param {string|object} param åé ajax æ¶é带çé¢å¤åæ°825 * @return {boolean} éªè¯ç»æ826 * @desc ajax å°å¼åéå°æå¡å¨ç«¯è¿è¡éªè¯ï¼æ¹æ³ä¸ºåæ¥827 * @todo æ¹æå¼æ¥828 * @todo å¯æ§è¡ jsonp829 * */830 , ajax: function(url, expected, type, param){831 var self = this832 , value = self.val()833 , name = self.attr('name')834 ;835 if( param ){836 if( typeof param === 'string' ){837 param += '&' + name + '=' + value;838 }839 else if( typeof param === 'object' ){840 param[name] = value;841 }842 }843 else{844 param = name + '=' + value;845 }846 return expected === $.ajax({847 url: url848 , type: (type ? type : 'GET')849 , data: param850 , async: false851 }).responseText;852 }853 /**854 * ç¹æ®éªè¯ç±»å855 * */856 /**857 * @method url858 * @memberof Validator.validMethods859 * @param {regexp=} regexp èªå®ä¹éªè¯æ£å表达å¼860 * @return {boolean} éªè¯ç»æ861 * @desc æ£éªæ¯å¦ä¸ºåæ³ urlï¼å¯ä»¥èªå®ä¹éªè¯æ£å表达å¼862 * */863 , url: function( regexp ){864 return (regexp || /^(?:http(?:s?):\/\/)?[a-z0-9\u4E00-\u9FA5\-_\.]*[a-z0-9\-_\u4E00-\u9FA5]+(?::(\d+))?\.[a-z0-9\u4E00-\u9FA5]+[0-9a-z\$\-\._'\(\)\/!\?#&\+=%\u4E00-\u9FA5]*/i).test( this.val() );865 }866 /**867 * @method email868 * @memberof Validator.validMethods869 * @param {regexp=} regexp èªå®ä¹éªè¯æ£å表达å¼870 * @return {boolean} éªè¯ç»æ871 * @desc æ£éªæ¯å¦ä¸ºåæ³é®ç®±ï¼å¯ä»¥èªå®ä¹éªè¯æ£å表达å¼872 * */873 , email: function( regexp ){874 return (regexp || /^(?:[a-z0-9_\-\.'])+@(?:[a-z0-9_\-\.])+\.(?:[a-z]{2,6})$/i).test( this.val() );875 }876 /**877 * @return {boolean} éªè¯ç»æ878 * @desc æ£æµæ¯å¦ä¸ºä¸æï¼æ£å表达å¼ä¸º /^[\u4E00-\u9FA5]*$/879 * */880 , isChinese: function(){881 return /^[\u4E00-\u9FA5]*$/.test( this.val() );882 }883 /**884 * @method idNumber885 * @memberof Validator.validMethods886 * @param {regexp=} regexp èªå®ä¹éªè¯æ£å表达å¼887 * @return {boolean} éªè¯ç»æ888 * @desc æ£æµæ¯å¦ä¸ºåæ³èº«ä»½è¯å·ç ï¼å¯ä»¥èªå®ä¹éªè¯æ£å表达å¼889 * */890 , idNumber: function( regexp ){891 return (regexp || /^\d{6}(?:((?:19|20)\d{2})(?:0[1-9]|1[0-2])(?:0[1-9]|[1-2]\d|3[0-1])\d{3}(?:x|X|\d)|(?:\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[1-2]\d|3[0-1])\d{3}))$/).test( this.val() );892 }893 /**894 * @method fileType895 * @memberof Validator.validMethods896 * @param {string=} type åæ°è³å°ä¸º 1 个 为æ件类ååç¼å ä¸å
å«ç¹897 * @return {boolean} éªè¯ç»æ898 * @desc æ£æµä¸ä¼ æ件æ¯å¦ä¸ºåæ³ç±»å899 * */900 , fileType: function(type){901 var result902 , argc = arguments.length903 , i = 0904 , regexp905 , val = this.val()906 ;907 if( this.is(':file') ){908 if( argc < 1 ){909 log('åæ°è®¾ç½®é误');910 result = false;911 }912 else{913 for(; i < argc; i++){914 if( !result ) break;915 regexp = new Regexp('\.'+ arguments[i] +'$', i);916 result = result && regexp.test( val );917 }918 }919 }920 else{921 log('æ ç¾é误');922 result = false;923 }924 return result;925 }926 /**927 * @method tel928 * @memberof Validator.validMethods929 * @return {boolean} éªè¯ç»æ930 * @desc éªè¯æ¯å¦ä¸ºçµè¯å·ç 931 * @todo å¾
å®æ932 * */933 , tel: function(){934 var result = false;935 // todo éªè¯çµè¯å·ç 936 return result;937 }938 /**939 * @method phone940 * @memberof Validator.validMethods941 * @return {boolean} éªè¯ç»æ942 * @desc éªè¯æ¯å¦ä¸ºææºå·ç 943 * @todo å¾
å®æ944 * */945 , phone: function(){946 var result = false;947 // todo éªè¯ææºå·ç 948 return result;949 }950 };951 $.validator = Validator;952 return Validator;...
commonlib.js
Source:commonlib.js
1/* IMPORTANT2 * =========3 * This script is included in the head section of every page.4 * This file should be kept small to not affect page load too bad.5 */6const validMethods = ['GET', 'POST'];7// Synchronous ajax requests...8// Asynchronous should be preferred.9// method = 'GET' or 'POST', url should be address pointing to localhost.10// Returns the ajax responseText or throws an Error.11function ajax(method, url) {12 // Function Guards13 if (!validMethods.includes(method)) {14 alert(`Unexpected request method: ${method.toString()}\nExpected one of: ${validMethods.toString()}`);15 throw new Error(`Unexpected request method: ${method.toString()}\nExpected one of: ${validMethods.toString()}`);16 }17 let ajaxRequest = new XMLHttpRequest();18 if (!ajaxRequest) {19 alert('Failed to create XMLHttpRequest (AJAX) instance!');20 throw new Error('Failed to create XMLHttpRequest (AJAX) instance!');21 }22 ajaxRequest.open(method, url, false);23 ajaxRequest.send();24 if (ajaxRequest.status === 200) {25 return ajaxRequest.responseText;26 } else {27 alert('Got non-200 (OK) HTTP status code in response.');28 throw new Error('Got non-200 (OK) HTTP status code in response.');29 }30}31/* Asynchronous via promises.32 * Returns a promise that contains the response text of the ajax request.33 */34function asyncAjax(method, url) {35 // Function Guards36 if (!validMethods.includes(method)) {37 return Promise.reject(`Unexpected request method: ${method.toString()}\nExpected one of: ${validMethods.toString()}`);38 }39 let promise = new Promise((resolve, reject) => {40 let ajaxRequest = new XMLHttpRequest();41 if (!ajaxRequest) {42 reject('Failed to create XMLHttpRequest (AJAX) instance!');43 }44 ajaxRequest.onreadystatechange = function() {45 // XMLHttpRequest.DONE === 446 if (ajaxRequest.readyState === 4) {47 if (ajaxRequest.status !== 200) {48 reject('Got non-200 (OK) HTTP status code in response.');49 } else {50 resolve(ajaxRequest.responseText);51 }52 }53 };54 ajaxRequest.open(method, url, true);55 ajaxRequest.send();56 });57 return promise;58}59window.onload = function() {60 console.log('hello');61 $('#dt').DataTable(62 {63 paging: false,64 searching: true,65 info: false66 }67 );68 document.querySelector('#dt_wrapper>div').style = 'min-width: 100%; margin-bottom: 5px;';69 document.querySelector('#dt_wrapper>div>div').style = 'max-width: 0%;';70 document.querySelector('#dt_wrapper').classList.add('justify-content-center');...
validMethods.js
Source:validMethods.js
1/**2 * @description Middleware que verifica si el método usado es válido para un URI.3 * Si no es válido, devuelve una respuesta 405 personalizada4 * 5 * @param validMethods Arreglo que contiene los métodos válidos.6 */7const validMethods = (validMethods = ["GET", "HEAD"]) => (req, res, next) => {8 if (validMethods.includes(req.method)){9 return next();10 }11 12 res.status(405);13 res.set("Allow",validMethods.join(", "));14 res.header("Content-Type", "application/problem+json");15 res.json({16 title: "Method not allowed",17 detail: `The method ${req.method} is not allowed for this resource`,18 status: 40519 });20};...
Using AI Code Generation
1var wpt = require('webpagetest');2var test = wpt('API_KEY');3test.validMethods(function(err, data) {4 if (err) {5 console.log('error: ' + err);6 } else {7 console.log(data);8 }9});10var wpt = require('webpagetest');11var test = wpt('API_KEY');12test.validLocations(function(err, data) {13 if (err) {14 console.log('error: ' + err);15 } else {16 console.log(data);17 }18});19var wpt = require('webpagetest');20var test = wpt('API_KEY');21test.validPrivateLocations(function(err, data) {22 if (err) {23 console.log('error: ' + err);24 } else {25 console.log(data);26 }27});28var wpt = require('webpagetest');29var test = wpt('API_KEY');30test.validConnectivity(function(err, data) {31 if (err) {32 console.log('error: ' + err);33 } else {34 console.log(data);35 }36});37var wpt = require('webpagetest');38var test = wpt('API_KEY');39test.validBrowsers(function(err, data) {40 if (err) {41 console.log('error: ' + err);42 } else {43 console.log(data);44 }45});46var wpt = require('webpagetest');47var test = wpt('API_KEY');48test.validTestOptions(function(err, data) {49 if (err) {
Using AI Code Generation
1var validMethods = require('webpagetest');2validMethods(function(err, methods) {3 console.log(methods);4});5var validLocations = require('webpagetest');6validLocations(function(err, locations) {7 console.log(locations);8});9var validConnectivity = require('webpagetest');10validConnectivity(function(err, connectivity) {11 console.log(connectivity);12});13var validBrowsers = require('webpagetest');14validBrowsers(function(err, browsers) {15 console.log(browsers);16});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!