DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> js文本框中禁止非數字字符輸入
js文本框中禁止非數字字符輸入
編輯:JavaScript綜合知識     

 <!DOCTYPE HTML>

<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="style.css">
</head>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<body>
    <input type="text" id="phone" value="0000">
</body>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        // Stuff to do as soon as the DOM is ready;
        var phone=$('#phone');
        $(phone).on('click',function(){
            phone.val('');
        })
        $(phone).on('keyup',function(evt){
            var phoneVal=phone.val();
            phoneVal=phoneVal.replace('/[^d]+/g', ''); //替換非數字字符為空格
            phoneVal=parseInt(phoneVal,10);
            if(isNaN(phoneVal)){
                phoneVal = '';
            }
            this.value=phoneVal;
        })
    });
</script>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved