使用javascript验证输入的字符量,文本长度和小数位 [英] Validate character amount, text length and decimal places from input using javascript
问题描述
我正在使用JavaScript来验证来自ASPxGridView控件(DevExpress组件)内部的文本框的输入.
I am using a javascript to validate input from a textbox that's inside a ASPxGridView control(DevExpress component).
我正在使用此javascript代码对其进行验证(通过OnKeyPress事件):
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 characters 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.
比这稍微复杂一点:此文本框的最大长度为6.我希望它仅允许两个小数位,并且最多允许三个数字在小数位前.例如:我希望它允许以下字符串:"123,12","45,32","7,65","9,6","85,32","94,1","310,2".我不希望它允许以下字符串:"1,123","125,789","1234,2"
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?
谢谢!
我试图使用你们让我喜欢的两个正则表达式:
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;
}
但是,即使输入与正则表达式匹配, regEx.test(text)
始终返回false.
However, regEx.test(text)
is always returning false, even when the input matches the regular expression.
另一项修改:
我从上面的代码中更改了RegExp对象的实例化:
I changed the instantiation of the RegExp object in the code above from this:
var regEx = new RegExp("/^\d{0,3}(,\d{0,2})?$/");
对此:
var regEx = /^\d{0,3}(,\d{0,2})?$/
现在可以了,谢谢!
推荐答案
/^\d{0,3}(,\d{0,2})?$/.test(textbox.value + char);
这将与最多三个小数位数匹配的任何数字匹配.(可选)它允许一个小数位和最多2个小数位.还匹配空字符串,以方便使用.因此,这将检查并确保结果框匹配.
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.
regEx的解释:
^
字符串开头
\d{0,3}
0至3位数字(含)
(...)?
可选组
,\d{0,2}
逗号后跟0到2位数字(包括0和2位数字)
A comma followed by 0 to 2 digits (inclusive)
$
字符串的结尾.
这篇关于使用javascript验证输入的字符量,文本长度和小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!