• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

pricing form, "$000.00" needs to display up to $520.00 dollars but can be any amount entered up to that amount with the decimal?-any ideas?

i have a form for a friend of mine that needs to display numbers for an accounting program up to 520.00 dollars. the decimal point needs to be there.  is there a way to limit the first digit to 0-5, the second 0-9, and third 0-9?. the .00 is not for entering-it is just behind the first 3 numbers. if i had to expand it to a thousand or hundred thousand digits, would that be easy too?-
0
toddkeller
Asked:
toddkeller
  • 6
  • 3
1 Solution
 
RoonaanCommented:
When you need it to be a clientside solution you could consider using javascript:

<script type="text/javascript">
 function checkform(frm)
 {
   //check the amount field
   if(!checkAmount(frm.amount))
     return false;
   
   return true;
 }
function checkAmount(amntField)
{
   if(!amntField.match(/^[0-5]{1}[0-9]{2}\.[0-9]{2}$/))
   {
       alert('You have to enter an amount between 0.00 and 520.00');
       amntField.focus();
       return false;
   }
   return true;
}
</script>
<form onsubmit="return checkform(this);">
  <input type="text" name="amount" maxsize="6" size="6" />
  <input type="submit" value="Submit" />
</form>

-r-
0
 
toddkellerAuthor Commented:
thanks so much!, that will work perfectly!.
t
0
 
toddkellerAuthor Commented:
roonaan, i think that i did not test your solution fully and i can't seem to get it to work when it goes beyond 520?- does it work right for you?- maybe i am srewing something up.?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RoonaanCommented:
Hi again,

There was indeed a grave error in the former code. However after some changes, and actually testing the code, I came up with the next which works.

function checkAmount(amntField)
{
   var val = amntField.value;
   var isIntVal = val.match(/^[0-9]{1,3}$/);
   var isFloatVal = val.match(/^[0-9]{1,3}\.[0-9]{1,2}$/)
   var isNum = isIntVal || isFloatVal;
   if(!isNum || val < 0 || val > 520)
   {
       alert('You have to enter an amount between 0.00 and 520.00');
       amntField.focus();
       return false;
   }
   return true;
}

This however allows any numeric value between 0.00 and 520.00 including values such as 10.1 or a plain 7. When you specifically need user to enter .00 or something at the end, please report back.

-r-
0
 
toddkellerAuthor Commented:
cool i will try this out tonight and get back to you ,
thanks!
t
0
 
toddkellerAuthor Commented:
Works great thanks!, i was wondering as a side note, is there a way to restrict users from entering letters at all and only numbers?-i know it's not a big deal but might make it easier if users accidentally hit letters?-what do you think?

Thanks for all the help i really appreciate it, and i learned alot from your example.-could you recomend a good online source for javascript?- i mean i use this site for sure!, but i was wondering if there was a good resource so that i could look up commands, their meaning and use?
thanks again,
t
0
 
RoonaanCommented:
Well, using javascript one could detect which key is pressed and possibly cancel that key. However there are some possible conflicts when choosing an approach as to which keys are allowed/disallowed:
- We define a set of allowed keys: Should we include hard return/backspace/tab?
- We define a set of disallowed keys: Which keys should we disallow?

<script type="text/JavaScript">
<!--
function doKey(key) {
  if (key >= 48 && key <= 57 || key == 46) {
          alert("You pressed a numeric key:" + key);
      //46 = dot.
  } elseif(key == 8 || key == 9 || key == 10 || key == 13) {
     //backspace=8
     //tab = 9
     //newline = 10
     //hard return = 13
  } else {
          return false;
  }
}
-->
</script>

<input type="text" onKeyPress="doKey(window.event.keyCode);" />
0
 
toddkellerAuthor Commented:
thanks!, i'll try that out and get back to you.-probably should not allow hard return/, backspace and tab are good.
-i imagine the main things to disallow are any letters, caps, shift, enter, alt,control, any of the f keys and windows keys?
0
 
toddkellerAuthor Commented:
-i'm not sure, i looked up the ascii:java glossary and saw what you were doing, but i'm not intelligent enough about the lanquage to code it up correctly.- i put it in conjuntion with a submit field to test on it's own and got bubcus.-when it works should it disallow you to even enter the letter keys?-like flash back to nothing once you enter them in?-just want to get a feel for what i am doing wrong.
-you know we could even take out the decimal entirely(46) and limit the text field entry to 3 numbers only(i did that and thought it might be simpler.)-but i can't get the dokey function itself to work-sorry man, i must be screwing it up somehow,
t
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now