如何验证小数值输入? [英] How to validate a decimal value input?

查看:83
本文介绍了如何验证小数值输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前只处理十进制值,在文本字段中用户只输入deciaml值。应该只接受10个数字,例如,如果用户输入12345.000000,它应该收到警告,说数字字段格式错误。请使用正确的格式重新输入。 (6.3)

Currently working on only decimal values where in the text field user enter only deciaml value. Should accept only 10 numbers for example if user enter 12345.000000 it should get an alert says Number field format error. Please re-enter using the proper format. (6.3)

使用我当前的jquery代码,它将接受小数值

With my current jquery code it will accept decimal value

$("#txtQty").keyup(function() {
var $this = $(this);
$this.val($this.val().replace(/[^\d.]/g, ''));        
});

这是我的html代码,这个html的文本框只允许10个字符

This is my html code for the text box with this html it will allow only 10 character

<input id="txtQty" type="text" maxlength="10"/>

我厌倦了以下SO用户告诉但我仍然遇到同样的问题

I tired the below SO user told but still I am getting the same issue

   $('#txtQty').focusout(function(e) {
   if('123456.789'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null){
      alert("Number field format error. Please re-enter using the proper format. (6.3)");  
   }else{
       '123456.7890'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null
   }
});

小数点前必须接受(1-6)小数点后6位数必须接受3只有零,如果输入错误,它应该发出一个警报,根本没有工作仍然没有得到那个

before decimal point it has to accept (1-6) 6 number after decimal point it has to accept 3 only zero's if wrongly typed it should throw an alert not at all working still not getting that

这是小提琴链接相同

123456.000 - 真实
12345.000 - true

1234.000 - true

123.000 - true

12.000 - true

1.000 - true

123456.000 -- true
12345.000 -- true
1234.000 -- true
123.000 -- true
12.000 -- true
1.000 -- true

提前致谢

$('.ipt_Havg').focusout(function (e) {
var regexp = /\d*\.\d{3}/
if (document.getElementById("ipt_Havg").value !== regexp) {
    alert("Number field format error. Please re-enter using the proper format. (6.3)");
} else {
    alert('nothing wrong here');
}
});


推荐答案

您可以在RegExps中指定重复:

You can specify repetitions in RegExps:

'123456.789'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null
true
'123456.7890'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null
false

这篇关于如何验证小数值输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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