Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency validation issue

Status
Not open for further replies.

ChrisQuick

Programmer
Oct 4, 1999
144
US
I am trying to modify the following code I got from javascriptsource.com. The code as written, give, an error if the value entered into the input is not a number or exceeds the specified numbers of decimal places. What I haven't been able to figure out is how to modify it to allow nulls. I really don't want to require that an amount be put in, I only to make sure that its a number and has no more than 2 decimal places and isn't a negative number either. Any help Available?

Here the script as is:


<!-- TWO STEPS TO INSTALL DECIMALS ALLOWED:

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->

<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- Original: Ronnie T. Moore, Editor -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! -->

<!-- Begin
function checkDecimals(fieldName, fieldValue) {

decallowed = 2; // how many decimals are allowed?

if (isNaN(fieldValue) || fieldValue == &quot;&quot;) {
alert(&quot;Oops! That does not appear to be a valid number. Please try again.&quot;);
fieldName.select();
fieldName.focus();
}
else {
if (fieldValue.indexOf('.') == -1) fieldValue += &quot;.&quot;;
dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);

if (dectext.length > decallowed)
{
alert (&quot;Oops! Please enter a number with up to &quot; + decallowed + &quot; decimal places. Please try again.&quot;);
fieldName.select();
fieldName.focus();
}
else {
alert (&quot;That number validated successfully.&quot;);
}
}
}
// End -->
</script>
</HEAD>
 
I am no expert, so take with caution. You say &quot;What I haven't been able to figure out is how to modify it to allow nulls.&quot;

Wouldn't this be achieved by removing
|| fieldValue == &quot;&quot;
from this line:
if (isNaN(fieldValue) || fieldValue == &quot;&quot;) {
?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top