Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript检查输入值_Javascript_Html - Fatal编程技术网

Javascript检查输入值

Javascript检查输入值,javascript,html,Javascript,Html,我正在寻找一个javascript函数,当使用onblur时,它会验证文本输入是一个只包含小数点或特殊字符的数字。我已经找到了一些东西,但到目前为止没有一件有效 以下是输入字段的内容: <tr> <td width="312"><strong>Cash on Hand</strong></td> <td width="188">$ <input type="text" onchange="updateasset

我正在寻找一个javascript函数,当使用onblur时,它会验证文本输入是一个只包含小数点或特殊字符的数字。我已经找到了一些东西,但到目前为止没有一件有效

以下是输入字段的内容:

  <tr>
<td width="312"><strong>Cash on Hand</strong></td>
<td width="188">$
  <input type="text" onchange="updateassets()" value="0" maxlength="11" name="CashOnHand" /></td>

库存现金
$


任何帮助都将不胜感激。

使用下面的方法
onKeyUp
事件,这将不允许在输入字段上使用任何字符

function numericOnly(e)
{
    var val = e.value.replace(/[^\d]/g, "");
    if(val != e.value)
        e.value = val;
}
输入字段代码

<input type="text" onchange="updateassets()" onKeyUp="numericOnly(this)" value="0" maxlength="11" name="CashOnHand" />

HTML

<form>
 <input type="text" id="txt" />
</form>
<input type="number" name="cashOnHand" value="0" maxlength="11" />

JS

(function(a) {
a.onkeypress = function(e) {
    if (e.keyCode >= 49 && e.keyCode <= 57) {}
    else {
        if (e.keyCode >= 97 && e.keyCode <= 122) {
            alert('Error');
            // return false;
        } else return false;
    }
};
})($('txt'));

function $(id) {
return document.getElementById(id);
}
 function checkInputInteger() {
        // Check if the input value is an integer
        if (this.value == parseInt(this.value)) {
            // The value is an integer
            console.log('Input ' + this.name + ' is an integer');
        }
        else {
            // The value is not an integer
            console.log('Input ' + this.name + ' is not an integer');
        }
    }

    // Get the input from DOM (getElementsByName returns a list)
    input = document.getElementsByName('cashOnHand')[0];
    // Bind the blur event to checkInputInteger
    input.addEventListener('blur', checkInputInteger, false);
(功能(a){
a、 onkeypress=功能(e){
如果(e.keyCode>=49&&e.keyCode=97&&e.keyCode给定:


使用更改事件是一个好主意,因为如果值没有更改,就不需要检查它。

我喜欢将结构(HTML)与函数(JS)分开。这就是为什么输入元素中没有“onchange”属性

HTML

<form>
 <input type="text" id="txt" />
</form>
<input type="number" name="cashOnHand" value="0" maxlength="11" />

onblur=“updateassets()”不工作?自动编辑用户输入非常烦人,并且keyup无法捕获使用菜单粘贴或拖动到字段的文本。使用
/\D/.test(e.value)
来确定值是否有效可能更简单。:-)好的捕获,将记住从菜单粘贴,否则它将与键盘一起工作。如果用户使用上下文菜单粘贴一个值怎么办?不会发送按键事件。
 function checkInputInteger() {
        // Check if the input value is an integer
        if (this.value == parseInt(this.value)) {
            // The value is an integer
            console.log('Input ' + this.name + ' is an integer');
        }
        else {
            // The value is not an integer
            console.log('Input ' + this.name + ' is not an integer');
        }
    }

    // Get the input from DOM (getElementsByName returns a list)
    input = document.getElementsByName('cashOnHand')[0];
    // Bind the blur event to checkInputInteger
    input.addEventListener('blur', checkInputInteger, false);