forked from wadackel/jquery-pwd-measure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.pwdMeasure.min.js
executable file
·8 lines (8 loc) · 5.57 KB
/
jquery.pwdMeasure.min.js
1
2
3
4
5
6
7
8
/*!
* jQuery.pwdMeasure
* jQuery plugin to measure the strength of the password.
* @version 1.0.5
* @author tsuyoshiwada
* @license MIT
*/
!function(e){"use strict";"function"==typeof define&&define.amd?define(["jquery"],e):"object"==typeof exports?module.exports=e(require("jquery")):e(jQuery)}(function(e){"use strict";function t(){this._initialize.apply(this,arguments)}function s(e,t,s){return Array.prototype.slice.call(e,t,void 0!==s?s:e.length)}function a(e){var t=e+"",s=t.charCodeAt(0);if(s>=55296&&56319>=s){var a=s;if(1===t.length)return s;var i=t.charCodeAt(1);return 1024*(a- -55296)+(i-56320)+65536}return s>=56320&&57343>=s?s:s}var i="1.0.5",r="pwdMeasure",n=12,l={VALID:1,INVALID:2,NOT_MATCH:3,EMPTY:4},c={score:100,label:"",className:""},o={minScore:50,minLength:6,events:"keyup change",labels:[{score:10,label:"とても弱い",className:"very-weak"},{score:30,label:"弱い",className:"weak"},{score:50,label:"平均",className:"average"},{score:70,label:"強い",className:"strong"},{score:100,label:"とても強い",className:"very-strong"},{score:"notMatch",label:"不一致",className:"not-match"},{score:"empty",label:"未入力",className:"empty"}],indicator:"#pm-indicator",indicatorTemplate:"パスワード強度: <%= label %> (<%= percentage %>%)",confirm:!1,onValid:!1,onInvalid:!1,onNotMatch:!1,onEmpty:!1,onChangeState:!1,onChangeValue:!1},h="pm";t.fn=t.prototype,t.fn._initialize=function(t,s){this.version=i,this.eventName="",this.percentage=0,this.chars={numbers:[],upperLetters:[],lowerLetters:[],specialChars:[]},this.status=l.INVALID,this.currentLabelObj={},this.options=s,this.options.labels=e.map(this.options.labels,function(t){return e.extend(!0,{},c,t)});var a;for(a=48;58>a;a++)this.chars.numbers.push(a);for(a=65;91>a;a++)this.chars.upperLetters.push(a);for(a=97;123>a;a++)this.chars.lowerLetters.push(a);for(a=32;48>a;a++)this.chars.specialChars.push(a);for(a=58;65>a;a++)this.chars.specialChars.push(a);for(a=91;97>a;a++)this.chars.specialChars.push(a);for(a=123;127>a;a++)this.chars.specialChars.push(a);this.$elem=t,this.$indicator=e(this.options.indicator),this.$confirm=e(this.options.confirm),this.$indicator.size()>0&&(this.indicatorDefaultHtml=this.$indicator.html()),this.update(!0),this._bindMethods()},t.fn.calc=function(){var t,s,i=0,r={numbers:0,upperLetters:0,lowerLetters:0,specialChars:0},l=this.$elem.val();for(i+=2*Math.floor(l.length/this.options.minLength),t=0;t<l.length;t++)s=a(l.charAt(t)),-1!==e.inArray(s,this.chars.numbers)&&r.numbers<2?(i++,r.numbers++):-1!==e.inArray(s,this.chars.upperLetters)&&r.upperLetters<2?(i++,r.upperLetters++):-1!==e.inArray(s,this.chars.lowerLetters)&&r.lowerLetters<2?(i++,r.lowerLetters++):-1!==e.inArray(s,this.chars.specialChars)&&r.specialChars<2&&(i++,r.specialChars++);i=i>n?n:i,this.percentage=Math.ceil(100*i/n),this.percentage=this.percentage>100?100:this.percentage},t.fn.getLabelIndex=function(t,s){var a=this,i=0;return t=t||a.percentage,s=s||a.status,e.each(a.options.labels,function(r,n){var c=a.options.labels[r-1]||{score:0,label:"",className:""};if(e.isNumeric(n.score)){if(!e.isNumeric(c.score))return!0;c.score=parseInt(c.score,10),n.score=parseInt(n.score,10),t>c.score&&t<=n.score&&(i=r)}else if("empty"===n.score&&s===l.EMPTY||"notMatch"===n.score&&s===l.NOT_MATCH)return i=r,!1}),i},t.fn.getLabelObj=function(e,t){var s=this.getLabelIndex(e,t);return this.options.labels[s]},t.fn.update=function(e){var t=this.status,s=this.$elem.val(),a=this.$confirm.val(),i=this.$confirm.size();e=e===!0?!0:!1,this.calc(),t=i>0?""!==a?s===a?this.percentage>=this.options.minScore?l.VALID:l.INVALID:l.NOT_MATCH:""===s?l.EMPTY:l.INVALID:this.percentage>=this.options.minScore?l.VALID:""===s?l.EMPTY:l.INVALID;var r=this.status!==t;if(this.status=t,this.currentLabelObj=this.getLabelObj(null,t),this._displayIndicator(),e||this._callbackApply(this.options.onChangeValue,this.percentage,this.currentLabelObj.label,this.currentLabelObj.className),r){var n,c=this.options,o=this.percentage,h=this.currentLabelObj;switch(this.status){case l.VALID:n="valid",this._callbackApply(c.onValid,o,h.label,h.className);break;case l.INVALID:n="invalid",this._callbackApply(c.onInvalid,o,h.label,h.className);break;case l.EMPTY:n="empty",this._callbackApply(c.onEmpty,o,h.label,h.className);break;case l.NOT_MATCH:n="notMatch",this._callbackApply(c.onNotMatch,o,h.label,h.className)}e||this._callbackApply(c.onChangeState,o,h.label,h.className,n)}},t.fn._displayIndicator=function(){if(0===this.$indicator.size())return!1;var t=this.options.indicatorTemplate,s={label:this.currentLabelObj.label,className:this.currentLabelObj.className,percentage:this.percentage};e.each(s,function(e,s){t=t.split("<%= "+e+" %>").join(s)}),t=t.replace(/(<%= .* %>?)/g,""),this.$indicator.html(t).removeClass(this._allLabelClass()).addClass(this.currentLabelObj.className)},t.fn._bindMethods=function(){var t=this;t.eventName=t.options.events||"keyup",t.eventName=t.eventName.replace(/([a-z]+)( ?)/gi,"$1."+h+"$2"),t.$elem.on(t.eventName,e.proxy(t.update,t)),t.$confirm.size()>0&&t.$confirm.on(t.eventName,e.proxy(t.update,t))},t.fn._unbindMethods=function(){},t.fn._callbackApply=function(){var t=arguments[0],a=s(arguments,1),i=e.isFunction(t)?t:function(){};return i.apply(this.$elem.get(0),a)},t.fn._allLabelClass=function(){var t=e.map(this.options.labels,function(e){return e.className});return t.join(" ")},t.fn.destroy=function(){this._unbindMethods(),this.$elem.removeData(r),this.$indicator.size()>0&&this.$indicator.html(this.indicatorDefaultHtml).removeClass(this._allLabelClass())},e.fn.pwdMeasure=function(s){return this.each(function(){e(this).data(r)||e(this).data(r,new t(e(this),e.extend(!0,{},o,s)))})}});