Javascript 如何使用Jquery验证输入文本框中的允许减号(-)和仅允许数字?

Javascript 如何使用Jquery验证输入文本框中的允许减号(-)和仅允许数字?,javascript,jquery,Javascript,Jquery,如何在金额输入字段中进行验证 在文本框中只允许使用减号(负号)一次。例如,-10.00。不允许使用其他特殊字符,因为它是金额字段 它不允许使用字母。只允许数字 小数点(.)在文本框中只能出现一次 编辑:我想这就是你要找的。在我的房间里找到这个。没有插件需要 HTML: <input type="text" maxlength="10" id="myInput"> }) 我刚刚根据您的情况编写了这篇文章,请在下面进行测试: 更新: 修改后可在jquery和javascript内联中

如何在金额输入字段中进行验证

  • 在文本框中只允许使用减号(负号)一次。例如,-10.00。不允许使用其他特殊字符,因为它是金额字段

  • 它不允许使用字母。只允许数字

  • 小数点(.)在文本框中只能出现一次


  • 编辑:我想这就是你要找的。在我的房间里找到这个。没有插件需要

    HTML:

    <input type="text" maxlength="10" id="myInput">
    

    })

    我刚刚根据您的情况编写了这篇文章,请在下面进行测试:

    更新: 修改后可在jquery和javascript内联中使用

    //这是一个中间件,它采用默认为2的“小数”作为参数
    currencyNumber=函数(小数){
    如果(小数类型!=='number'){
    小数=2;
    }
    返回函数(e){
    变量输入=$(此窗口实例?e:e.currentTarget);
    var值=$.trim(input.val());
    var HasNegativeEnumber=value.substr(0,1)='-'?'-':'';
    var nextValue=价值
    .替换(/\.+/g,“.”)
    .替换(/[^0-9\.]+/g');
    如果(nextValue=='.| |(nextValue.length>1&&nextValue===“00”)){
    nextValue='';
    }
    var dotsFound=nextValue.split('.').filter(函数(i){
    返回i.长度;
    });
    var afterDot='';
    var beforeDot='';
    如果(dotsFound.length>1){
    beforeDot=dotsFound[0];
    点结拼接(0,1);
    afterDot=dotsFound.join(“”);
    nextValue=+(beforeDot)+'.+afterDot.substr(0,小数);
    }
    if(nextValue.substr(nextValue.length-1,1)='.')){
    input.one('change',function(){
    if(nextValue.substr(nextValue.length-1,1)='.')){
    nextValue=nextValue.substr(0,nextValue.length-1);
    input.val(hasNegativeEnumber+nextValue);
    }
    $(此).off('change');
    })
    }否则{
    input.off('change')
    }
    input.val(hasNegativeEnumber+nextValue);
    };
    }
    //这里是您称之为中间件的地方
    美元(“#金额”)。在(“keyup”,currencyNumber(3))
    
    
    测试您的号码(绑定来自jquery):
    
    测试您的号码(绑定来自javascript内联):
    使用
    type=“number”
    这将减少减号和小数的数量。我的要求与该示例不匹配。这是匹配的,但如何使用它?是这个插件还是?完全像这个,我想没有插件怎么做?是的,很好。但是这个代码在我的页面中不起作用。你能检查一下我哪里出错了吗?我把代码贴在上面了code@Diamond再次复制该函数,它不适合html,我现在修复了它,所以您的内联代码应该是这个
    onkeyup=“货币编号(3)(本);“
    ,您需要再次调用它,这次使用
    this
    。第一对“()”是“currencyNumber”函数,带小数的,第二对“()”表示需要
    对象的函数。在jquery中,您不需要调用第二对“()“因为jQuery会这样做,并且默认情况下会将
    this
    对象传递给它。再次复制函数我刚才更改了它,或者只是将
    if
    替换为
    if(nextValue=='.| |(nextValue.length>1&&nextValue==“00”){nextValue=''.}”
    我还需要一个帮助。我有另一个移动字段。这个字段只允许数字,但应该允许空格。例如:055 789655像这样。@Diamond这是断章取义的,如果你需要移动字段的函数,你应该再发一篇帖子
    currencyNumber()
    用于设置货币/moneyz的格式。
    var input = document.getElementById("myInput");
    
    input.onkeypress = function(e) {    e = e || window.event;
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    
    // Allow non-printable keys
    if (!charCode || charCode == 8 /* Backspace */ ) {
        return;
    }
    
    var typedChar = String.fromCharCode(charCode);
    
    // Allow numeric characters
    if (/\d/.test(typedChar)) {
        return;
    }
    
    // Allow the minus sign (-) if the user enters it first
    if (typedChar == "-" && this.value == "") {
        return;
    }
    
    // In all other cases, suppress the event
    return false;