Jump to content

Allowing numbers and decimal points only javascript function help!


McMaster

Recommended Posts

I am using a javascript function to check whether a price field is numeric or not. Now the problem is that it won't let decimal points to go through.

 

So, it's okay to do 100 but not 100.50. As you can see, it doesn't allow the decimal point. Does anyone have any idea what I should do to this function so it allows decimal points. Here is the code:

 

<script type='text/javascript'>
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            alert('You are only allowed to enter numbers.'); return false;
         } else {
            return true;
         }
      }

Link to comment
Share on other sites

Well, personally I would only validate on submission of the form instead of trying to limit input. But, you can modify that function pretty easily to also accept decimal points. But, that won't prevent users from entering invalid number (e.g. 123.4567, 123.45.67, etc). So, I would take a two-pronged approach: 1) Only allow numbers and periods to be entered and then on submission validate that the value is a valid number with no more than two decimal places:

 

<html>
<head>
<script type="text/javascript">
function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    return (charCode<=31 ||  charCode==46 || (charCode>=48 && charCode<=57));
}

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

function validateForm(formObj)
{
    if(!validCurrency(document.getElementById('amount').value))
    {
        alert('You must enter a valid dollar amount.');
        document.getElementById('amount').select();
        document.getElementById('amount').focus();
        return false;
    }
    return true;
}
</script>
</head>

<body>
<form name="test" action="" onsubmit="return validateForm(this);">
Amount: <input type="text" id="amount" onkeypress="return isNumberKey(this);">
<br />
<button type="submit">submit</button>
</form>

</body>
</html>

Link to comment
Share on other sites

minor caveat..that would allow someone to enter in "100."  (a dollar amount and a decimal but no cents).  I don't know how often that would *really* come up but you should be aware that it is possible.  You can easily automatically look for and trim it off if it's there instead of making the user jump through that hoop though...

 

Also it would allow for "100.1" (vs. 100.10).  Technically the "100.1" is valid and the same according to the computer but most people write it with the 0.  Mostly a matter of philosophy but again, just wanted to make sure you are aware that is possible. 

 

To fix both of these with the current javascript code, you can change {0,2} to {2} and that will force the user to enter in 2 digits after the decimal.  But again, there are ways to fix it where it doesn't involve making the user do it, so yeah...up to you. 

Link to comment
Share on other sites

minor caveat..that would allow someone to enter in "100."  (a dollar amount and a decimal but no cents).  I don't know how often that would *really* come up but you should be aware that it is possible.  You can easily automatically look for and trim it off if it's there instead of making the user jump through that hoop though...

 

Also it would allow for "100.1" (vs. 100.10).  Technically the "100.1" is valid and the same according to the computer but most people write it with the 0.  Mostly a matter of philosophy but again, just wanted to make sure you are aware that is possible. 

 

To fix both of these with the current javascript code, you can change {0,2} to {2} and that will force the user to enter in 2 digits after the decimal.  But again, there are ways to fix it where it doesn't involve making the user do it, so yeah...up to you. 

 

Yeah, I thought about all of that when I posted the code. I figured that something like "100.", "100.1", and ".1" were all valid currency values that could be logically interpreted by the code correctly. However, the real "issue" with the code I posted is that a single decimal point would also pass the validation. I figured the processing code could be written to interpret that value as zero.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.