Javascript中具有唯一小数点的纯数字文本字段

Javascript中具有唯一小数点的纯数字文本字段,javascript,Javascript,我可以用Javascript开发下面的代码,它只允许将数字添加到给定的HTMLTextField function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode; if (charCode > 31 && (charCode < 48 || charCode > 57)) { return false; }

我可以用Javascript开发下面的代码,它只允许将数字添加到给定的HTML
TextField

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
        return false;
    }
    return true;
}

<input type='text' id='textField' name='textField' onkeypress="return isNumberKey(event);">
函数isNumberKey(evt)
{
var charCode=(evt.which)?evt.which:event.keyCode;
如果(字符码>31&(字符码<48 | |字符码>57))
{
返回false;
}
返回true;
}

但是我需要允许在
文本字段中输入一个且仅一个小数点()。我尝试了一些方法,但我没有编造出符合这一要求的代码。我怎样才能只允许给定的
文本字段中带有唯一的小数点的数字?

你能试试这个javascript吗

<script type="text/javascript">
   function isNumberKey(evt)
   {
 var charCode;
 if(evt.keyCode) //For IE
   charCode = evt.keyCode;
 else if(evt.Which)
   charCode = evt.Which; // For FireFox
 else
   charCode = evt.charCode; // Other Browser

     //var charCode = (evt.which) ? evt.which : event.keyCode;
     return (charCode<=31 ||  charCode==46 || (charCode>=48 && charCode<=57));
   }

   function validCurrency(txt)
   {
      return txt.match(/^\d*(.\d{0,2})?$/);
   }

   function validateForm(formObj)
   {
     if(!validCurrency(document.getElementById('textField').value))
     {
       document.getElementById('textField').select();
       document.getElementById('textField').focus();
       return false;
     }
     return true;
  }
</script>

<form name="form1" action="" onsubmit="return validateForm(this);">
    Amount: <input type='text' id='textField' name='textField' onkeypress="return isNumberKey(event);">

    <button type="submit">submit</button>
</form>

函数isNumberKey(evt)
{
var字符码;
if(evt.keyCode)//用于IE
charCode=evt.keyCode;
else if(evt.Which)
charCode=evt.Which;//对于FireFox
其他的
charCode=evt.charCode;//其他浏览器
//var charCode=(evt.which)?evt.which:event.keyCode;

return(charCode=48&&charCode你能试试这个javascript吗

<script type="text/javascript">
   function isNumberKey(evt)
   {
 var charCode;
 if(evt.keyCode) //For IE
   charCode = evt.keyCode;
 else if(evt.Which)
   charCode = evt.Which; // For FireFox
 else
   charCode = evt.charCode; // Other Browser

     //var charCode = (evt.which) ? evt.which : event.keyCode;
     return (charCode<=31 ||  charCode==46 || (charCode>=48 && charCode<=57));
   }

   function validCurrency(txt)
   {
      return txt.match(/^\d*(.\d{0,2})?$/);
   }

   function validateForm(formObj)
   {
     if(!validCurrency(document.getElementById('textField').value))
     {
       document.getElementById('textField').select();
       document.getElementById('textField').focus();
       return false;
     }
     return true;
  }
</script>

<form name="form1" action="" onsubmit="return validateForm(this);">
    Amount: <input type='text' id='textField' name='textField' onkeypress="return isNumberKey(event);">

    <button type="submit">submit</button>
</form>

函数isNumberKey(evt)
{
var字符码;
if(evt.keyCode)//用于IE
charCode=evt.keyCode;
else if(evt.Which)
charCode=evt.Which;//对于FireFox
其他的
charCode=evt.charCode;//其他浏览器
//var charCode=(evt.which)?evt.which:event.keyCode;

return(charCode=48&&charCode你能试试这个Javascript吗?它在我的例子中有效:

var decimalPointCount = 0; 
        function checkInputData(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                if (charCode == 46 && decimalPointCount == 0) {
                    decimalPointCount++;
                    return true;
                }
                else {
                    return false;
                }
            }
            return true;
        }
var分点计数=0;
函数checkInputData(evt){
var charCode=(evt.which)?evt.which:event.keyCode;
如果(字符码>31&(字符码<48 | |字符码>57)){
if(charCode==46&&decimalPointCount==0){
小数点计数++;
返回true;
}
否则{
返回false;
}
}
返回true;
}

你能试用一下这个Javascript吗?它适用于我的情况:

var decimalPointCount = 0; 
        function checkInputData(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                if (charCode == 46 && decimalPointCount == 0) {
                    decimalPointCount++;
                    return true;
                }
                else {
                    return false;
                }
            }
            return true;
        }
var分点计数=0;
函数checkInputData(evt){
var charCode=(evt.which)?evt.which:event.keyCode;
如果(字符码>31&(字符码<48 | |字符码>57)){
if(charCode==46&&decimalPointCount==0){
小数点计数++;
返回true;
}
否则{
返回false;
}
}
返回true;
}

它允许输入多个小数点。小数点不能重复,也不能是第一个字符,并且不能在Mozilla Firefox上使用。Hi Lion,在IE和Chrome中,事件解析为window.event。Firefox没有此属性。这是不在Firefox上使用的原因。我将尝试使用javascript。Hi now使用编辑过的脚本进行检查。需要执行第一位小数点检查。它允许输入多个小数点。小数点不能重复,也不能是第一个字符,并且不能在Mozilla Firefox上工作。嗨,Lion,在IE和Chrome中,事件解析为window.event。Firefox没有此属性。这是原因是无法在Firefox上工作。我将尝试使用javascript.hi,现在使用已编辑的脚本进行检查。需要进行第一位小数点检查。