943,748 Members | Top Members by Rank

Ad:
Apr 25th, 2008
0

decimal (10,2) limit

Expand Post »
How to limit the user to decimal (10,2) when he enters number in the textfield?

Can anyone show how to do it in javascript?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trient is offline Offline
13 posts
since Apr 2008
Apr 26th, 2008
0

Re: decimal (10,2) limit

Please specify a bit more.
Do you already have a sort of validation function?
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008
May 1st, 2008
0

Re: decimal (10,2) limit

no i dont have validation function
how to restrict so that the currency can be limit to (10,2) decimal
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trient is offline Offline
13 posts
since Apr 2008
May 1st, 2008
0

Re: decimal (10,2) limit

From your message, I conclude that your problem is that Javascript does not understand that for you, "10,2" equals "10.2".
Here I wrote a sample html, but the important part is the javascript function that checks the value.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>No title</title>
  6. <script type="text/javascript" language="javascript">
  7. function curcheck(curf,maxval) {
  8.  
  9. textcur = curf.value.toString();
  10.  
  11. textcur += '';
  12. x = textcur.split(',');
  13. textcur = x[0]+'.'+x[1];
  14. if(Number(textcur) > maxval) {
  15. say('result',false);
  16. return
  17. }
  18. say('result',true);
  19. }
  20.  
  21. function say(were,val) {
  22.  
  23. text = ( val ) ? "Valid value entered" : "Invalid value entered" ;
  24. eval("document.getElementById('"+were+"').innerHTML = text");
  25.  
  26. }
  27. </script>
  28. </head>
  29.  
  30. <body>
  31. <form id="myform" action="" >
  32. <input type="text" size="4" id="currency" onchange="curcheck(this,10.2);" />
  33. <p id="result">&nbsp;</p>
  34. </form>
  35. </body>
  36. </html>

Hope this is what you need.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008
May 1st, 2008
0

Re: decimal (10,2) limit

You would test the form field with a regex like this.
(10, 2) in database means 10 numbers, 2 decimal places. so 12345678.90 is valid.
javascript Syntax (Toggle Plain Text)
  1. var testRegEx = /[0-9]{1,8}\.?[0-9]{0,2}/
  2. var isValidNumber = form.formField.value.match(testRegEx);
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Drop Down box AJAX
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: unVisible Listbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC