表單輸入控制

<script>
    function ValidateSpecialCharacter() {
       var code; 
       if (document.all) { //判斷是否是IE瀏覽器
        code = window.event.keyCode; 
       } else { 
        code = arguments.callee.caller.arguments[0].which; 
       } 
       var character = String.fromCharCode(code);
       var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\'',\\;,\\=,\"]"); 
       //特殊字符正則表達式 
       if (txt.test(character)) {
        if (document.all) { 
         window.event.returnValue = false; 
        } else { 
         arguments.callee.caller.arguments[0].preventDefault(); 
        } 
       }
    }
    // 驗證中文字符和特殊字符 
    function chineseVaildate(value){
       if (value == null || value=="")
        return true;
         if ((/[\u4E00-\u9FA5]+/.test(value))){
        return false;
       }
       return true;
    }
    function validate(obj){
       if (!chineseVaildate(obj.value)){
        alert("有特殊字符和中文字符");
       }
    }
    //驗證是否數字
    function check(obj){ 
        if (isNaN(obj.value)) {
            alert("請輸入數字!"); 
            obj.value="";
        } 
    } 
</script>
<input type="text" name="tt"  οnkeyup="check(this);">

//不允許輸入字符和空格
<input id="code" οnkeypress="return ValidateSpecialCharacter();" οnblur="validate(this)"/>

//不允許輸入空格
<input type="text" name="username" οnkeyup="value=value.replace(/\s/g,'')"/>

//只能輸入數字
<input type="text" id="q11" size="14" maxlength="14" οnkeyup='this.value=this.value.replace(/\D/gi,"")'>

//只能輸入正數,負數,小數
οnkeyup="value=value.replace(/[^\-?\d.]/g,'')"

//只能輸入正數,小數
οnkeyup="value=value.replace(/[^\d.]/g,'')"
<input type="number" placeholder="請輸入數字">

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章