JS+CSS設置text只能輸入數字


<SCRIPT LANGUAGE="JavaScript">
<!--
    var isIE = false;
    var isFF = false;
    var isSa = false;
    if ((navigator.userAgent.indexOf("MSIE") > 0)
            && (parseInt(navigator.appVersion) >= 4))
        isIE = true;
    if (navigator.userAgent.indexOf("Firefox") > 0)
        isFF = true;
    if (navigator.userAgent.indexOf("Safari") > 0)
        isSa = true;
    function onlyNumber(e) {
        var key;
        iKeyCode = window.event ? e.keyCode : e.which;
        if (!(((iKeyCode >= 48) && (iKeyCode <= 57)) || (iKeyCode == 13)
                || (iKeyCode == 46) || (iKeyCode == 45) || (iKeyCode == 37)
                || (iKeyCode == 39) || (iKeyCode == 8)))
        {
            if (isIE) {
                e.returnValue = false;
            } else {
                e.preventDefault();
            }
        }
    }
//-->
</SCRIPT>

 

<input type="text" onkeypress="return onlyNumber(event)">

 

<SCRIPT LANGUAGE="JavaScript">
<!--
    var isIE = false;
    var isFF = false;
    var isSa = false;
    if ((navigator.userAgent.indexOf("MSIE") > 0)
            && (parseInt(navigator.appVersion) >= 4))
        isIE = true;
    if (navigator.userAgent.indexOf("Firefox") > 0)
        isFF = true;
    if (navigator.userAgent.indexOf("Safari") > 0)
        isSa = true;
    function onlyNumber(e,val) {
        
        var key;
        iKeyCode = window.event ? e.keyCode : e.which;
        if (!(((iKeyCode >= 48) && (iKeyCode <= 57)) || (iKeyCode == 13)
                || (iKeyCode == 46) || (iKeyCode == 8)))
        {
            if (isIE) {
                e.returnValue = false;
            } else {
                e.preventDefault();
            }
        }
        var char_str = val.split("");
        var k = 0;
        for(var i=0;i<char_str.length;i++){
            if(char_str[i] == '.') k++;
        }
        if(k > 0 && (iKeyCode == 46)){
            if (isIE) {
                e.returnValue = false;
            } else {
                e.preventDefault();
            }
        }
        var str = val.split(".");
        if(str[1] != null && str[1].length > 1){
            if (isIE) {
                e.returnValue = false;
            } else {
                e.preventDefault();
            }
        }
    }
//-->
</SCRIPT>

 

<s:textfield name="mgrPlan.mgrDisVal" cssClass="text-input validate[required]" 
id="mgrDisVal" cssStyle="width:200px" 
onkeypress="return onlyNumber(event,this.value)">
</s:textfield>

 






另外一個:

=====================

<input type="text" onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');
if(this.value.split('.').length>2){
this.value=this.value.split('.')[0]+'.'+this.value.split('.')[1]}">
 <%= text_field_tag "close_sight",4,:onKeyPress => "return onlyNumber(event);", :style => "ime-mode:disabled"%>

 


  
  
  
          
//正整數 
 function onlyNum(obj){
    console.log(obj.value);
    obj.value = obj.value.replace(/[^\d]/gi, "");
}


//正、負整數
function numHasPlus(obj){
    //取第一位數字別人
    var t = obj.value.charAt(o);
   
    //替換掉不是數字部分
    var _value = obj.value.replace(/[^\d]/gi, "");
   
    //如果是負數
    if (t == '-') {
        _value = '-' + _value;
    }
   
    //頁面
    if (obj.value == _value)
        return;
    obj.value = _value;
}


//小數
function onlyFloat(obj){
    //    obj.value = obj.value.replace(/[^(?\d+)(\.\d+)?]/gi, "");
    var regex = /^[+|-]?\d*\.?\d*$/;
    if (regex.match(obj.value)) {
        return;
    }
    var o_value = obj.value.replace(/[^(?\d+)(\.\d+)?]/gi, "");
    if (o_value == '' || o_value == null) {
        obj.value = 0;
        return;
    }
    o_value = o_value.toString();
    var o_array = o_value.split('.');
    var m_value = 0.0;
    if (o_array.size() <= 1) {
        m_value = parseFloat(o_array[0]);
    }
    else {
        m_value = parseFloat(Nan(o_array[0] + '.' + o_array[1]));
    }
    if (obj.value == m_value)
        return;
    obj.value = m_value;
}

 

<INPUT CLASS="textbox" TYPE="text" STYLE="ime-mode:disabled">

 

不管你進來的時候是什么輸入法,輸入的都是英文

ime-mode語法:

ime-mode : auto | active | inactive | disabled
取值:
auto :  默認值。不影響IME的狀態。與不指定 ime-mode 屬性時相同
active :  指定所有使用IME輸入的字符。即激活本地語言輸入法。用戶仍可以撤銷激活IME
inactive :  指定所有不使用IME輸入的字符。即激活非本地語言。用戶仍可以撤銷激活IME
disabled :  完全禁用IME。對於有焦點的控件(如輸入框),用戶不可以激活IME

text-transform語法:

 
        
text-transform : none | capitalize | uppercase | lowercase
取值:
none :  默認值。無轉換發生
capitalize :  將每個單詞的第一個字母轉換成大寫,其余無轉換發生
uppercase :  轉換成大寫
lowercase :  轉換成小寫


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM