js 特殊值文字框輸入驗證

來源:互聯網
上載者:User
<!--僅允許輸入數值-->
<public:attach event="onpaste" onevent="checkPaste()" />
<public:attach event="onkeypress" onevent="checkInput()" />
<public:attach event="onfocus" onevent="getFocus()" />
<public:attach event="onblur" onevent="lostFocus()" />
<script language="javascript">
function checkInput(){
    var k = event.keyCode;
    switch(element.className) {
        case "IntegerOnly":
            /*
             只允許輸入 數字 和 小數點,否則 disableInput
             13 是回行
            */
            if ( ( k < 48   || k > 57 ) && k != 13  ){
                disableInput();
            }
            break;
        case "DecimalOnly":
            /*
             只允許輸入 數字 ,否則 disableInput
             13 是回行
             46 是小數點
            */
            if ( ( k < 48   || k > 57 ) && k != 13  &&   k != 46 ) {
                disableInput();
            }
            break;
        case "UserCode":
            /*
             只允許輸入英文字母、底線、數字 ,否則 disableInput
             13 是回行
             46 是小數點
            */
            if ( ( k < 97   || k > 122 )  // 小寫字母
            &&   ( k < 65   || k > 90 )   // 大寫字母
            &&   ( k < 48   || k > 57 )   // 數字
            &&     k != 13 ) {            // 回行
                disableInput();
            }
            break; 
    }
}
/*
 * 粘貼時激發的事件
 * 調用的filtrateInput在resume.aspx中
 * 放棄了
 */
function checkPaste() {
    return false;
    ctrlID = element.id;
    setTimeout("filtrateInput(\""+ctrlID+"\")",1);
}
function disableInput() {
    if( element.className == "IntegerOnly"
     || element.className == "DecimalOnly"
     || element.className == "UserCode" )
    event.returnValue = false;
}
function getFocus() {
    if( element.type == "text" 
     || element.type == "password"
     || element.type == "textarea"  )
     {
        if( !(element.readOnly) ) {
            element.style.backgroundColor = "#DCF1FA";
        }
     }
}
function lostFocus() {
    if( element.type == "text" 
     || element.type == "password"
     || element.type == "textarea"  )
     {
        element.style.backgroundColor = "";
     }
}
</script>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.