十进制验证OnKeypress所有浏览器Javscript [英] Decimal Validation OnKeypress all browser Javscript

查看:77
本文介绍了十进制验证OnKeypress所有浏览器Javscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在0到100的onkeypress中进行十进制验证,其中两位小数位允许一个点.
下面的代码在IE中工作,也允许多个点.它不会触发Firefox和Chrome.请帮帮我.

I have required decimal validation in onkeypress with 0 to 100 with two decimal places allow one dot.
the below code is working in IE also allow multiple dot. It does not fire Firefox and Chrome. please help me.

function numbersonly(e,control)
{
    //alert('numbersonly')
    var unicode=e.charCode? e.charCode : e.keyCode
    if (unicode!=8 && unicode!=46) //if the key isn't the backspace key (which we should allow)
    {
           if (unicode<48||unicode>57) //if not a number
           return false //disable key press
           var character = String.fromCharCode(unicode);
           var val = control.value+character

           if(val>100)
           {
               return false
           }

           if(String(val).indexOf(".")!=-1)
           {
               if (String(val).indexOf(".")<String(val).length–3)
               {
                   return false
               }
           }
    }
}



编辑-在代码中添加了适当的缩进以使其更具可读性. -Ankur



Edit - Added proper indentation to the code to make it more readable. - Ankur

推荐答案

JavaScript无法读懂您的思想,因此不可避免地会出现多个点,否则会给用户带来混乱.允许它们,并在使用整个字符串时添加另一个验证.如果转换失败,您可以通过捕获异常来进行验证.此外,您可以执行进一步的验证,例如有效范围.您可以对服务器端进行验证,这更容易且不会影响性能.

让我在一个用例上说明这一点,以表明不可避免地会出现多个点.

考虑到用户已经放置了所有数字和一个点并想要更改位置.有两种方法:1)删除一个点,然后将另一个点放在不同的位置; 2)首先添加第二个点,然后删除错误的点.

情况1仍然不会造成任何问题.在情况2中:如果您允许用户放置第二个点(允许多个点),则有效性受到侵犯:此时有两个点;您可以稍后如上所述进行验证; 2)您计算出在文本中已经找到一个点,并阻止添加第二个点.这会使用户非常困惑.有时输入点,有时不输入. 我的键盘坏了吗?.".

—SA
Javascript cannot read your mind, so multiple dots are unavoidable or else the user experience will be confusing. Allow them, and add another validation of the whole string at the moment it is used. You can validate by catching exception if conversion was failed. Additionally, you can perform further validation, such as valid range. You can do validation of server side, which is easier and would not compromise performance.

Let me illustrate this on a use case to show that multiple dots are unavoidable.

Consider the user already put all digits and one dot and want to change is position. There are two ways: 1) remove one dot and put another dot in a different place; 2) add second dot first and then remove the wrong one.

The case #1 would not cause any problems anyway. In the case #2: if your allow the user to put second dot (allow multiple dot), the validity is violated: at this moment there are two dots; you can validate it later as described above; 2) you calculate that one dot is already found in the text and block adding a second dot. This would confuse the user very much. Sometimes the dot is entered, sometimes not. "Is my keyboard broken?..".

—SA


感谢您提出的问题
并使用Regex解决此问题,您可以学习regex 此处并验证您的regex
Thanks for the question
and to solve this problem use Regex u can learn regex Here and validate your regex Here


这篇关于十进制验证OnKeypress所有浏览器Javscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆