驗(yàn)證碼組件

組件辑奈,依賴jquery

css

.code-input {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}
.code-input .code-container{
    display: flex;
    flex-wrap: nowrap;
}
.code-input .code-container .tb-code{
    box-sizing: border-box;
    border: solid 0.05rem #E7E8EB;
    border-right: 0;
    font-size: 0.9rem;
    text-align: center;
    background-color: transparent;
    color: #333333;
}
.code-input .code-container .tb-code:first-child{
    border-radius: 0.15rem 0 0 0.15rem;
}
.code-input .code-container .tb-code:last-child{
    border-radius: 0 0.15rem 0.15rem 0;
    border-right: solid 1px #E7E8EB;
}

js

;(function () {
    'use strict';
    function setAttributes(target, option) {
        Object.keys(option).forEach(function (key) {
            target.setAttribute(key, option[key])
        })
        return target
    }
    function createElement(tagName, option) {
        var dom = document.createElement(tagName)
        setAttributes(dom, option)
        return dom
    }
    function focusHandler(e) {
        this.input.focus();
    }
    function setVal(val) {
        var value = val.slice(0,this.codeLength) || ''
        for(var i=0;i<this.codeLength;i++){
            this.tabs[i].value = value[i] || '';
        }
        this.input.value = value
        this.currentVal = value
    }
    function inputHandler(e) {
        setVal.call(this, e.currentTarget.value)
    }
    function keyupHandler(e) {
        if(e.key === 'Enter' && this.currentVal.length===this.codeLength) {
            this.input.blur();
            if(Object.prototype.toString.call(this.option.oncomplete)==="[object Function]") {
                this.option.oncomplete.call(this, this.currentVal)
            }
        }else {
            return true
        }
    }
    function CodeInput(el, option){
        var target = null;
        if(el){
            target = el && el[0] ? el[0] : el
            if(target.nodeType !== 1){
                target = document.querySelector(target)
            }
        }
        if(!target){
            console.error('codeinput初始化失敗拴魄,缺少初始化dom節(jié)點(diǎn)')
            return
        }
        this.currentVal = ''
        this.el = target
        this.option = option
        this.codeLength = option.codeLength || 6
        this.flag = true
        this.init()
    }
    CodeInput.prototype.init = function () {
        // 創(chuàng)建dom結(jié)構(gòu)
        this.el.setAttribute('class',this.el.getAttribute('class') + ' code-input')
        this.input = createElement('input',
            {
                    id:'codeInput',
                    name: this.option.name || 'codeInput',
                    style: 'width: 100%;opacity: 0;height: 100%;position: absolute;top: 0;'
                }
            )
        this.tabsContainer = createElement('div',
            {
                id:'code-container',
                class: 'code-container'
            }
        )
        this.tabs = []
        for (var i=0; i<this.codeLength;i++){
            this.tabs[i] = createElement('input',
                {
                    id:'tb'+i,
                    disabled: 'disabled',
                    class: 'tb-code',
                    style: 'width:'+this.option.width+';height:'+this.option.height
                }
            )
            this.tabsContainer.append(this.tabs[i])
        }
        this.el.append(this.tabsContainer)
        this.el.append(this.input)
        this.addEventListener()
    }
    CodeInput.prototype.addEventListener = function () {
        var _self = this
        this.input.onkeyup = function (e) {
            keyupHandler.call(_self,e)
        }
        if(this.option.hasChinese) {
            $(this.input).on('compositionstart', function(){
                _self.flag = false;
            })
            $(this.input).on('compositionend', function(e) {
                _self.flag = true;
                inputHandler.call(_self, e)
            })
        }
        $(this.input).on('input', function (e) {
            if(_self.option.hasChinese){
                if(_self.flag){
                    inputHandler.call(_self, e)
                }
            }else {
                inputHandler.call(_self, e)
            }
        })
        this.tabsContainer.onclick = function (e) {
            focusHandler.call(_self,e)
        }
    }
    CodeInput.prototype.val = function (setValue) {
        if(setValue===undefined){
            return this.currentVal
        }
        setVal.call(this, setValue)
    }
    if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {

        // AMD. Register as an anonymous module.
        define(function() {
            return CodeInput;
        });
    } else if (typeof module !== 'undefined' && module.exports) {
        module.exports = FastClick.attach;
        module.exports.FastClick = FastClick;
    } else {
        window.CodeInput = CodeInput;
    }
}())

使用方式

// 驗(yàn)證碼位數(shù)
var repayCodeLength = 6,
var imageCode = $('#imageCode')
function oncomplete() {
    imageCode.focus()
}
repayCodeInstance = new CodeInput(repayCode,{
    width: '2.3775rem;',
    height: '2.2rem',
    name: 'repayCode',
    codeLength: repayCodeLength,
    hasChinese: false,
    oncomplete: oncomplete
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末甘苍,一起剝皮案震驚了整個(gè)濱河市愉老,隨后出現(xiàn)的幾起案子驶兜,更是在濱河造成了極大的恐慌吞鸭,老刑警劉巖珊蟀,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件菊值,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡育灸,警方通過(guò)查閱死者的電腦和手機(jī)腻窒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)磅崭,“玉大人儿子,你說(shuō)我怎么就攤上這事≌莱希” “怎么了典徊?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵杭煎,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我卒落,道長(zhǎng)羡铲,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任儡毕,我火速辦了婚禮也切,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘腰湾。我一直安慰自己雷恃,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布费坊。 她就那樣靜靜地躺著倒槐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪附井。 梳的紋絲不亂的頭發(fā)上讨越,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天,我揣著相機(jī)與錄音永毅,去河邊找鬼把跨。 笑死,一個(gè)胖子當(dāng)著我的面吹牛沼死,可吹牛的內(nèi)容都是我干的着逐。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼意蛀,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼耸别!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起浸间,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤太雨,失蹤者是張志新(化名)和其女友劉穎吟榴,沒(méi)想到半個(gè)月后魁蒜,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡吩翻,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年兜看,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狭瞎。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡细移,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出熊锭,到底是詐尸還是另有隱情弧轧,我是刑警寧澤雪侥,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站精绎,受9級(jí)特大地震影響速缨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜代乃,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一旬牲、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧搁吓,春花似錦原茅、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至摩骨,卻和暖如春贝室,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背仿吞。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工滑频, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人唤冈。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓峡迷,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親你虹。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绘搞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容