McMaster Posted October 31, 2010 Share Posted October 31, 2010 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; } } Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 1, 2010 Share Posted November 1, 2010 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> Quote Link to comment Share on other sites More sharing options...
McMaster Posted November 1, 2010 Author Share Posted November 1, 2010 Thanks for that solution. It worked great Quote Link to comment Share on other sites More sharing options...
.josh Posted November 1, 2010 Share Posted November 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 1, 2010 Share Posted November 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.