如何验证具有两个小数点的浮点数? [英] How Do I Validate Floating Point Number which will have two decimal points?

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

问题描述

Hello All,



我是Javascript的新手。我想写一个javascript函数来验证以下条件下的浮点数:



1)这个数字不应该大于100.

2)只允许两个小数点,例如: - 50.75,1.75,100.00

3)除了其他三位数字之外,只允许三位数字100 - 100.00

4)如果我们输入两个以上的十进制数字,如10.123456,它应该删除最后一个十进制数字,并保持数字像10.12



你可以帮我吗?



在此先感谢。

Venkata Ghanitala。

Hello All,

I am new to Javascript. I want to write a javascript function to validate floating point numbers with below conditions:

1) The number should not be greater than 100.
2) Only two decimal points are allowed, ex:- "50.75", "1.75", "100.00"
3) Only three digit number "100" is allowed apart from other three digit numbers - 100.00
4) If we enter more than two decimal numbers like "10.123456", it should remove the last decimal numbers and keep the number like "10.12"

Can you please help me in this?

Thanks In Advance.
Venkata Ghanitala.

推荐答案

您好,



感谢您的回复,



我可以限制小数点数只允许2点,但是如何限制小数点前的数字不大于100?



代码是:



Hello,

Thanks for your reply,

I am able to restrict decimal poit numbers to allow only 2 points, but how can i restrict the number to be not greater than 100 before decimal point?

The code is:

// Allocation Percentage text-box allow numeric and allow 2 decimal points only
 function extractNumber(obj, decimalPlaces, allowNegative) {
     var temp = obj.value;

     // avoid changing things if already formatted correctly
     var reg0Str = '[0-9]*';
     if (decimalPlaces > 0) {
         reg0Str += '\[\,\.]?[0-9]{0,' + decimalPlaces + '}';
     } else if (decimalPlaces < 0) {
         reg0Str += '\[\,\.]?[0-9]*';
     }
     reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
     reg0Str = reg0Str + '


';
var reg0 = new RegExp(reg0Str);
if (reg0.test(temp)) return ;

// 首先替换所有非数字
var reg1Str = ' [^ 0-9' + (decimalPlaces!= 0 ' 。'' ')+(decimalPlaces!= 0 ' ,'' ')+(allowNegative?' - '' ')+ ' ]';
var reg1 = new RegExp(reg1Str, ' g');
temp = temp.replace(reg1,' ');

if (allowNegative){
// 替换额外的否定
var hasNegative = temp.length > 0 && temp.charAt( 0 )== ' - '< /跨度>;
var reg2 = / - / g;
temp = temp.replace(reg2,' ');
if (hasNegative)temp = ' - ' + temp;
}

if (decimalPlaces!= 0 ){
var reg3 = /[\,\\\]/g;
var reg3Array = reg3.exec(temp);
if (reg3Array!= null ){
// 仅保留第一次出现。
// 以及decimalPlaces指定的位数或整个字符串(如果decimalPlaces< 0
var reg3Right = temp.substring(reg3Array.index + reg3Array [ 0 ]长度);
reg3Right = reg3Right.replace(reg3,' ');
reg3Right = decimalPlaces > 0 ? reg3Right.substring( 0 ,decimalPlaces):reg3Right;
temp = temp.substring( 0 ,reg3Array.index)+ ' 。' + reg3Right;
}
}

obj。 value = temp;
}
function blockNonNumbers(obj,e,allowDecimal,allowNegative){
var key;
var isCtrl = false ;
var keychar;
var reg;
if (window。 event ){
key = e.keyCode;
isCtrl = window。 event .ctrlKey
}
else < span class =code-keyword> if
(e.which){
key = e.which;
isCtrl = e.ctrlKey;
}

if (isNaN(key)) return < span class =code-keyword> true
;

keychar = String .fromCharCode(key);

// 检查退格或删除,或按Ctrl键
if (key == 8 || isCtrl){
返回 true ;
}

reg = / \d /;
var isFirstN = allowNegative? keychar == ' - '&& obj。 value .indexOf(' - ')== -1: false ;
var isFirstD = allowDecimal? keychar == ' 。'&& obj。 value .indexOf(' 。')== -1: false ;
var isFirstC = allowDecimal? keychar == ' ,'&& obj。 value .indexOf(' ,')== -1: false ;
return isFirstN || isFirstD || isFirstC || reg.test(keychar);
}
function blockInvalid(obj){
var temp = obj。 value < /跨度>;
if (temp == - ){
temp = ;
}

if (temp.indexOf( )== temp.length - 1 && temp.indexOf( )!= -1){
temp = temp + 00;
}
if (temp.indexOf( )== 0 ){
temp = 0 + temp;
}
if (temp.indexOf( )== 1 && temp.indexOf( - )== 0 ){
temp = temp.replace( - - 0);
}
if (temp.indexOf( )== temp.length - 1 && temp.indexOf( )!= -1){
temp = temp + 00;
}
if (temp.indexOf( )== 0 ){
temp = 0 + temp;
}
if (temp.indexOf( )== 1 && temp.indexOf( - )== 0 ){
temp = temp.replace( - - 0);
}
temp = temp.replace( );
obj。 value = temp;
}
'; var reg0 = new RegExp(reg0Str); if (reg0.test(temp)) return true; // first replace all non numbers var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (decimalPlaces != 0 ? ',' : '') + (allowNegative ? '-' : '') + ']'; var reg1 = new RegExp(reg1Str, 'g'); temp = temp.replace(reg1, ''); if (allowNegative) { // replace extra negative var hasNegative = temp.length > 0 && temp.charAt(0) == '-'; var reg2 = /-/g; temp = temp.replace(reg2, ''); if (hasNegative) temp = '-' + temp; } if (decimalPlaces != 0) { var reg3 = /[\,\.]/g; var reg3Array = reg3.exec(temp); if (reg3Array != null) { // keep only first occurrence of . // and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0 var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length); reg3Right = reg3Right.replace(reg3, ''); reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right; temp = temp.substring(0, reg3Array.index) + '.' + reg3Right; } } obj.value = temp; } function blockNonNumbers(obj, e, allowDecimal, allowNegative) { var key; var isCtrl = false; var keychar; var reg; if (window.event) { key = e.keyCode; isCtrl = window.event.ctrlKey } else if (e.which) { key = e.which; isCtrl = e.ctrlKey; } if (isNaN(key)) return true; keychar = String.fromCharCode(key); // check for backspace or delete, or if Ctrl was pressed if (key == 8 || isCtrl) { return true; } reg = /\d/; var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false; var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false; var isFirstC = allowDecimal ? keychar == ',' && obj.value.indexOf(',') == -1 : false; return isFirstN || isFirstD || isFirstC || reg.test(keychar); } function blockInvalid(obj) { var temp = obj.value; if (temp == "-") { temp = ""; } if (temp.indexOf(".") == temp.length - 1 && temp.indexOf(".") != -1) { temp = temp + "00"; } if (temp.indexOf(".") == 0) { temp = "0" + temp; } if (temp.indexOf(".") == 1 && temp.indexOf("-") == 0) { temp = temp.replace("-", "-0"); } if (temp.indexOf(",") == temp.length - 1 && temp.indexOf(",") != -1) { temp = temp + "00"; } if (temp.indexOf(",") == 0) { temp = "0" + temp; } if (temp.indexOf(",") == 1 && temp.indexOf("-") == 0) { temp = temp.replace("-", "-0"); } temp = temp.replace(",", "."); obj.value = temp; }





和来自Textbox的调用方法是:





and calling methods from Textbox is:

<asp:TextBox ID="txtAllocationPercentage" runat="server" CssClass="textbox" ToolTip="Enter Allocation Percentage"

                                            onblur="extractNumber(this,2,true);blockInvalid(this);" onkeyup="extractNumber(this,2,true);" 

                                            onKeyPress="return blockNonNumbers(this, event, true, true);"></asp:TextBox>                                            



请给我你的建议,我在哪里可以修改代码来检查不超过100的数字?



谢谢。


Please give me your suggestions, where can i modify the code to check the number not greater than 100?

Thanks.


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

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