ChrisQuick
Programmer
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="JavaScript">
<!-- 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 == "" {
alert("Oops! That does not appear to be a valid number. Please try again."
fieldName.select();
fieldName.focus();
}
else {
if (fieldValue.indexOf('.') == -1) fieldValue += ".";
dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);
if (dectext.length > decallowed)
{
alert ("Oops! Please enter a number with up to " + decallowed + " decimal places. Please try again."
fieldName.select();
fieldName.focus();
}
else {
alert ("That number validated successfully."
}
}
}
// End -->
</script>
</HEAD>
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="JavaScript">
<!-- 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 == "" {
alert("Oops! That does not appear to be a valid number. Please try again."
fieldName.select();
fieldName.focus();
}
else {
if (fieldValue.indexOf('.') == -1) fieldValue += ".";
dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);
if (dectext.length > decallowed)
{
alert ("Oops! Please enter a number with up to " + decallowed + " decimal places. Please try again."
fieldName.select();
fieldName.focus();
}
else {
alert ("That number validated successfully."
}
}
}
// End -->
</script>
</HEAD>