1. 程式人生 > >input輸入框只能輸入正整數、或輸入正整數和小數

input輸入框只能輸入正整數、或輸入正整數和小數

input 輸入數字類的一些判別 

方法一:實現輸入框只能輸入正整數,輸入同時禁止了以0開始的數字輸入,防止被轉化為其他進位制的數值。

<input type='text' onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')">

方法二: 實現輸入框只能輸入正整數

<input type="text"name="price"id="price"value=""
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 
onkeyup="value=value.replace(/[^\d.]/g,'')" />
方法三:限制文字框只能輸入正數,負數,小數
<input type="text" onkeyup="value=value.replace(/[^\-?\d.]/g,'')" />
方法四:
1.限制文字框只能輸入正數,小數
<input type="text" onkeyup="value=value.replace(/[^\d.]/g,'')" />

2.只能輸入數字,能輸小數點.
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name="txt1" onchange="if(/\D/.test(this.value)){alert('只能輸入數字');this.value='';}">

3.數字和小數點
<input type="text" t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value"onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}">

4.文字框只能輸入數字程式碼(小數點也不能輸入)
<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">

方法五:input輸入框加入限制只能輸入正整數,輸入其他字元會自動清除: 
html的寫法:

<input type="text" id="price"
    onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"  
    onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'0')}else{this.value=this.value.replace(/\D/g,'')}" />  

js裡面也要寫:

$("#totalCnt").change(function () {
        var totalCnt = $("#totalCnt").val();
        if (totalCnt != parseInt(totalCnt)){
            $.sobox.alert(
                    '溫馨提示',
                    '請輸入正確的正整數',
                    function () {
                        $("#totalCnt").val("");
                    }
            )
            return false;
        }
    })

 input框對字母、漢字、符號的一些判別


1.只能輸入字母和漢字
<input onkeyup="value=value.replace(/[\d]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))" maxlength="10" name="Numbers">
2只能輸入英文字母和數字,不能輸入中文
<input onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">

3.只能輸入數字和英文<font color="Red">chun</font>
<input onkeyup="value=value.replace(/[^\d|chun]/g,'')">

4.小數點後只能有最多兩位(數字,中文都可輸入),不能輸入字母和運算子號:
<input onkeypress="if((event.keyCode<48 || event.keyCode>57) &amp;&amp; event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
<br><br>
5.小數點後只能有最多兩位(數字,字母,中文都可輸入),可以輸入運算子號:
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
6.輸入中文:
<input type="text" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')">  

7.輸入數字:  
<input type="text" onkeyup="this.value=this.value.replace(/\D/g,'')">  

8.輸入英文:  
<input type="text" onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')">  
 <br><br>
9.三個合在一起  
<input onkeyup="value=value.replace(/[^\w\u4E00-\u9FA5]/g, '')">  

10.只輸入數字和字母  
:<input class="input" maxlength="12" size="15" name="username" id="username" onkeyup="value=value.replace(/[\W]/g,'')"> 
11.除了英文的標點符號以外 其他的人都可以中文,英文字母,數字,中文標點
<input type="text" onkeyup="this.value=this.value.replace(/^[^[email protected]#$%^&amp;*()-=+]/g,'')">

相關推薦

input輸入只能輸入整數輸入整數小數

input 輸入數字類的一些判別  方法一:實現輸入框只能輸入正整數,輸入同時禁止了以0開始的數字輸入,防止被轉化為其他進位制的數值。 <input type='text' onkeyup="value=value.replace(/^(0+)|[^\d]+/g,

input輸入只能輸入整數字母小數漢字

只需將需要的程式碼加入到input輸入框中,即可使用! 1,文字框只能輸入數字程式碼(小數點也不能輸入) 程式碼如下: <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value

JS通過則限制 input 輸入只能輸入整數小數(金額或者現金) 兩位小數

第一: 限制只能是整數 ? 1 <input type = "text" name= "number" id = 'number' onkeyup= "if(! /^d+$/.t

input輸入只能輸入整數

input輸入框加入限制只能輸入正整數,輸入其他字元會自動清除: html的寫法: <input type="text" id="price" onkeyup="if(this.val

jquery控制輸入只能輸入0整數

<input type="num" name="group_user_num" class="form-control" size="10" style="ime-mode:Disabled" placeholder="只能輸入數字" value="2" onkey

JavaScript控制輸入只能輸入非負整數

1、問題背景       問題:一個輸入框,輸入的是月份,保證輸入的內容只能是非負正整數 2、JavaScript程式碼       function checkMonth()     {    

input 輸入 只能輸入數字字母漢字等

from:https://www.cnblogs.com/phpfensi/p/7298617.html 1.文字框只能輸入數字程式碼(小數點也不能輸入) <input onkeyup="this.value=this.value.replace(/\D/g,'')"

iOS設定輸入只能輸入整數小數,且小數位數不能超過兩位

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ // 限制只能輸

表單驗證input 輸入 只能輸入數字字母漢字等

1.文字框只能輸入數字程式碼(小數點也不能輸入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')

限制輸入只能輸入整數

<input type="number" min="0" max="100" step="1" onkeyup="if(this.value.length==1){this.value=this

輸入只能輸入整數的最簡實現

最簡單的html程式碼實現輸入框只能輸入正整數,輸入同時禁止了以0開始的數字輸入,防止被轉化為其他進位制的數值。 <input type='text' onkeyup="value=valu

購物車中的input輸入只能輸入數字輸入為0的時候默認為1

pos put brush pre tex || lac ext html <input type="text" value="1" onkeyup="value=(parseInt((value=value.replace(/\D/g,‘‘))==‘‘||pars

在HTML中限制input 輸入只能輸入純數字

限制 input 輸入框只能輸入純數字 1. onkeyup = "value=value.replace(/[^\d]/g,'')" 使用 onkeyup 事件,有 bug ,那就是在中文輸入法狀態下,輸入漢字之後直接回車,會直接輸入字母   2.

input 輸入只能輸入數字,長度為2(也可以修改任意長度)

直接放在input標籤裡面 1.input長度為2 oninput="if(value.length>2)value=value.slice(0,2)"  2.控制input輸入數字 on

js實現input輸入只能輸入數字的功能(完美測試通過)

<input type="text" style="ime-mode:disabled;" onpaste="return false;" onkeypress="keyPress()" /> function keyPress() { var k

input 輸入只能輸入純數字

1、onkeyup = "value=value.replace(/[^\d]/g,'')" 使用 onkeyup 事件,有 bug ,那就是在中文輸入法狀態下,輸入漢字之後直接回車,會直接輸入字母   2、onchange = "value=value

input 輸入只能輸入數字,長度為2(也可以修改任意長度);input只能輸入數字小數

直接放在input標籤裡面 1.input長度為2 oninput="if(value.length>2)value=value.slice(0,2)"  2.控制input輸入數字 onkeyup="if(this.value.length==1){this.v

前端頁面中input輸入只能輸入數字

input輸入框只能輸入數字 在實際的專案中,有很多輸入框,比如說年齡欄位,身高欄位等等,要求使用者輸入的是純數字,為了簡單,我們可以直接在input標籤中新增onkeyup對輸入的內容進行驗證,如下程式碼: <input type="text" placehold

使用則表示式限制輸入只能輸入數字

正則表示式限制輸入框只能輸入數字      程式碼如下: <input type="text" onkeyup="this.value=this.value.replace(/[^/d]/g,'') " onafterpaste="this.value=this.v

js jquery 限制input輸入只能輸入兩位小數的數字

//正整數 兩位小數 $('.inputWrap').on('input', function () { var num = $(this).val(); if(n