Click here to Skip to main content
15,883,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 30-May-14 4:08am
v2
Comments
What to help? What have you tried? Where is the issue?
vbmike 30-May-14 10:24am    
There are conditional statements you could use as well as websites that discuss number formatting, rounding etc that you should go to. It may be you could use a 'switch' statement. Give folks some code to look at and then they will respond possibly.

1 solution

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:

C#
// 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 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;
 }


and calling methods from Textbox is:

ASP.NET
<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>                                            

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

Thanks.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900