帮助改进这个javascript输入掩码 [英] Help to improve this javascript input mask
本文介绍了帮助改进这个javascript输入掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
功能
- 只有数字
- 唯一的点(。)
但是,我需要改进,允许用户数字只有两个数字在点 / p>
有效的例子:
- 121213
- 123450.10
- 12345678910111213.39
当前代码(从这个,不需要保持完全相同的代码... ):
< HTML>
< HEAD>
< SCRIPT language = Javascript>
<! -
函数isNumberKey(evt,obj){
var containsDot = obj.value.indexOf(。);
var charCode =(evt.which)? evt.which:event.keyCode
if(charCode> 31&&(charCode< 48 || charCode> 57)){
if(charCode == 46&& < 0){
return true;
}
返回false;
}
返回true;
}
// - >
< / SCRIPT>
< / HEAD>
< BODY>
< INPUT id =txtCharonkeypress =return isNumberKey(event,this)type =textname =txtChar>
< / BODY>
< / HTML>
解决方案
我已经发布了一个完整的工作版本: http://jsfiddle.net/georgecalm/NRtVs/2/
HTML:
< input id =txttype =textname =txtChar/>
JS:
code> var isStrValid = function(str){
return((str.match(/ [^ \d ^。] /)=== null)
&&(str .replace(/\d+\.?\d?\d?/,)===));
};
var node = dojo.byId(txt);
dojo.connect(node,onkeyup,function(){
if(!isStrValid(node.value)){
node.value = node.value.substring(0,node .value.length-1);
}
});
(第一个检查(isStrValid))确保没有任何数字和点。第二个检查模式。
Features
- Only numbers
- A unique dot (.)
But...i need improve to allow user digits only two numbers after the dot
Valid examples:
- 121213
- 123450.10
- 12345678910111213.39
The current code (adapted from this, not need to keep exactly the same code...) :
<HTML>
<HEAD>
<SCRIPT language=Javascript>
<!--
function isNumberKey(evt,obj){
var containsDot = obj.value.indexOf(".");
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)){
if(charCode == 46 && containsDot < 0){
return true;
}
return false;
}
return true;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<INPUT id="txtChar" onkeypress="return isNumberKey(event,this)" type="text" name="txtChar">
</BODY>
</HTML>
解决方案
I've posted a fully working version of this here: http://jsfiddle.net/georgecalm/NRtVs/2/
HTML:
<input id="txt" type="text" name="txtChar" />
JS:
var isStrValid = function(str) {
return ((str.match(/[^\d^.]/) === null)
&& (str.replace(/\d+\.?\d?\d?/, "") === ""));
};
var node = dojo.byId("txt");
dojo.connect(node, "onkeyup", function() {
if (!isStrValid(node.value)) {
node.value = node.value.substring(0, node.value.length-1);
}
});
The first checks (of the isStrValid) makes sure you don't have anything but numbers and a dot. The second checks the pattern.
这篇关于帮助改进这个javascript输入掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文