Solved

# 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?

Posted on 2005-04-24
215 Views
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
Question by:toddkeller

LVL 49

Accepted Solution

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

LVL 1

Author Comment

thanks so much!, that will work perfectly!.
t
0

LVL 1

Author Comment

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

LVL 49

Expert Comment

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

LVL 1

Author Comment

cool i will try this out tonight and get back to you ,
thanks!
t
0

LVL 1

Author Comment

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

LVL 49

Expert Comment

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

LVL 1

Author Comment

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

LVL 1

Author Comment

-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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.