Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

my requirement is text box is there inside grid. For this textbox, i need validation like this-- before decimal user should able to enter 5 digit(should not allow to type more than 5) and after decimal 3 digit. How to do using javascript


C#
function IsDecimal(evt) {
            debugger;
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
                return false;
            else {
                var len = evt.value.length;
                var index = evt.value.indexOf('.');

                if (index > 0 && charCode == 46) {
                    return false;
                }

                if (index < 0) {
                    if (len > 5) {
                        return false;
                    }
                }


                    if (index > 0) {
                        var CharAfterdot = (len + 1) - index;
                        var CharBeforeDot = (len + 1) - CharAfterdot;
                        if (CharAfterdot > 4) {
                            return false;
                        }
                        if (CharBeforeDot > 5) {
                            return false;
                        }

                    }


            }
            return true;
        }


Here its allowing to user to enter till 5 digit before decimal.But problem is after that i m trying to put dot which is decimal.Its not allowing to enter to user into textbox
Posted
Updated 6-Jun-14 4:15am
v2

Try this:
XML
<!DOCTYPE html>
<html>
<body>
Textbox:
<input type="text" onkeyup="validate(this);" onkeydown="validate(this);">

<script>
function validate(e) {
   var isValidCharacter = true;
   e.value = e.value.trim();
   var inputValue = e.value;
   if(isNaN(inputValue)) {
      isValidCharacter = false;
   } else if (inputValue.length == 6 && inputValue.substr(inputValue.length - 1) != '.') {
      isValidCharacter = false;
   } else if (inputValue.length > 9) {
      isValidCharacter = false;
   }

   if(isValidCharacter == false) {
      e.value = inputValue.substring(0, inputValue.length - 1);
   }
}
</script>

</body>
</html>
 
Share this answer
 
Comments
Snesh Prajapati 6-Jun-14 14:01pm    
Superb answer !!
try with regex
JavaScript
var regex = /^[0-9]{0,5}(?:\.[0-9]{0,3})?$/;
return regex.test(evt.value);
 
Share this answer
 
Comments
kamalsekhar 6-Jun-14 10:52am    
Hi DamithSL,
Its not working. While typing its allowing user to enter more that 5 before decimal.
Try this method below and call it from onkeypress event.

JavaScript
function isNumberAndDecimal(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode != 45 && (charCode != 46 || $(this).val().indexOf('.') != -1) &&
            (charCode < 48 || charCode > 57))
        return false;

    return true;
}


XML
<asp:TextBox ID="txtQuantity" runat="server" MaxLength="10" Width="60px" onkeypress="return isNumberAndDecimal(event);"></asp:TextBox>
 
Share this answer
 

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