验证使用JavaScript的输入量卡拉科特,文本长度和小数 [英] Validate caracter amount, text length and decimal places from input using javascript

查看:140
本文介绍了验证使用JavaScript的输入量卡拉科特,文本长度和小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是JavaScript从一个文本框,这是一个ASPxGridView控制(DevEx preSS组件)的内部验证输入。

我使用这个JavaScript code,以验证它(直通安其preSS事件):

 函数CheckKey(S,E){
    VAR键= ASPxClientUtils.GetKey code(e.htmlEvent);
    VAR CHAR = String.fromChar code(键);
    如果(e.htmlEvent.shiftKey){
        如果(!IsAvailableChar(炭))
            ASPxClientUtils preventEvent(e.htmlEvent)。
    }其他
        如果ASPxClientUtils preventEvent(e.htmlEvent)((IsAvailableChar(炭)|| IsAvailableKey(密钥))!)。    返回;
}功能IsAvailableChar(炭){
    VAR AvailableChars =0123456789;
    返回AvailableChars.indexOf(炭)!= -1;
}功能IsAvailableKey(键){    开关(键){
        案例46://删除
            返回true;
            打破;
        案例37://左箭头
            返回true;
            打破;
        案例39://右箭头
            返回true;
            打破;
        案例16://移
            返回true;
            打破;
        案例188://逗号
            返回true;
            打破;
        默认:
            返回false;
            打破;
    }

我用这块的一些caracters,它工作正常。
但现在我想要做一些稍微有点复杂:我不希望用户在文本框中输入一个以上的逗号。

一个有点复杂多了:这个文本框的最大长度为6。我希望它只允许两位小数,之前小数位的最大三个数字。
例如:
我希望它让这些字符串:123,12,45,32,7.65,9.6,85,32,94,1,310,2。
我不希望它让这些字符串:1123,125789,1234,2

我如何能做到这一点任何想法?

感谢您!

编辑:

我试图用两个普通的前pressions你们告诉我是这样的:

 函数CheckKey(S,E){
    VAR键= ASPxClientUtils.GetKey code(e.htmlEvent);
    VAR CHAR = String.fromChar code(键);    VAR文本= document.getElementsByName(s.uniqueID)[0] .value的字符+;    VAR正则表达式=新的RegExp(/ ^ \\ D {0,3}(,\\ D {0,2})$ /?);    如果(regEx.test(文本))
        返回;
    其他
        ASPxClientUtils preventEvent(e.htmlEvent)。    返回;
}

然而, regEx.test(文本)总是返回假的,即使在输入正规前pression相匹配。

另一个编辑:

我改变了RegExp对象的实例在code以上从这个:

  VAR的regex ​​=新的RegExp(/ ^ \\ D {0,3}(,\\ D {0,2})$ /?);

要这样:

  VAR正则表达式= / ^ \\ d {0,3}(,\\ D {0,2})?$ /

而现在它的工作,谢谢!


解决方案

  / ^ \\ d {0,3}(,\\ D {0,2})?$ /测试(textbox.value +字符);

这将匹配任意数量多达三个pre-小数。或者,它允许十进制和最多2位小数。也匹配空字符串,以方便使用。因此,这将检查以确保最终箱相匹配。

正则表达式的解释:

  ^

字符串的开始

  \\ D {0,3}

0至3个数字(含)

 (...)?

这是可选的组

 ,\\ D {0,2}

一个逗号后面跟着0到2位数(含)

  $

字符串的结尾。

I am using a javascript to validate input from a textbox that's inside a ASPxGridView control(DevExpress component).

I am using this javascript code to validate it(thru OnKeyPress event):

function CheckKey(s, e) {
    var key = ASPxClientUtils.GetKeyCode(e.htmlEvent);
    var char = String.fromCharCode(key);
    if (e.htmlEvent.shiftKey) {
        if (!IsAvailableChar(char))
            ASPxClientUtils.PreventEvent(e.htmlEvent);
    } else
        if (!(IsAvailableChar(char) || IsAvailableKey(key))) ASPxClientUtils.PreventEvent(e.htmlEvent);

    return;
}

function IsAvailableChar(char) {
    var AvailableChars = "0123456789,";
    return AvailableChars.indexOf(char) != -1;
}

function IsAvailableKey(key) {

    switch (key) {
        case 46: //delete
            return true;
            break;
        case 37: //left arrow
            return true;
            break;
        case 39: //right arrow
            return true;
            break;
        case 16: //shift
            return true;
            break;
        case 188: //comma
            return true;
            break;
        default:
            return false;
            break;
    }

I use this to "block" some caracters and it works fine. But now I wanna do something a little bit more complicated: I don't want the user to input more than one comma in the textbox.

A little bit more complicated than that: the maxlength of this textbox is 6. I want it to allow two decimal places only and the maximum of three numbers before the decimal places. For example: I want it to allow these strings: "123,12", "45,32", "7,65", "9,6", "85,32", "94,1", "310,2". I don't want it to allow these strings: "1,123", "125,789", "1234,2"

Any ideas on how I can do that?

Thank you!

Edit:

I tried to use the two regular expressions you guys told me to like this:

function CheckKey(s, e) {
    var key = ASPxClientUtils.GetKeyCode(e.htmlEvent);
    var char = String.fromCharCode(key);

    var text = document.getElementsByName(s.uniqueID)[0].value + char;

    var regEx = new RegExp("/^\d{0,3}(,\d{0,2})?$/");

    if(regEx.test(text))
        return;
    else
        ASPxClientUtils.PreventEvent(e.htmlEvent);

    return;
}

However, regEx.test(text) is always returning false, even when the input matches the regular expression.

Another edit:

I changed the instantiation of the RegExp object in the code above from this:

var regEx = new RegExp("/^\d{0,3}(,\d{0,2})?$/");

To this:

var regEx = /^\d{0,3}(,\d{0,2})?$/

And now it worked, thank you!

解决方案

/^\d{0,3}(,\d{0,2})?$/.test(textbox.value + char);

This will match any number with as many as three pre-decimal places. Optionally, it allows a decimal and up to 2 decimal places. Also matches the empty string, for ease of use. So this will check to make sure the resultant box matches.

An explanation of the regEx:

^

Start of string

\d{0,3}

0 to 3 digits (inclusive)

(...)? 

An optional group

,\d{0,2}

A comma followed by 0 to 2 digits (inclusive)

$

End of string.

这篇关于验证使用JavaScript的输入量卡拉科特,文本长度和小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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