decimals allowed




JavaScript Source Code 3000: Forms: Decimals Allowed







































Decimals Allowed







Allows the user to enter a number with up to 2 decimal places in a text box. In other words, 99 is ok, 99.9 is ok, 99.99 is ok, but 99.999 is rejected. You can easily change the number of decimal places that are permitted. (i.e. 1, 2, 3, etc.) For the example, enter a number with up to 2 decimal places then try entering one with more than 2 decimal places.






Please enter a number with up to 2 decimal places:








JavaScript Source Code 3000: Forms: Decimals Allowed
Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Apple-s). The script is yours!!!





    





<!-- 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 -->

<! >
<! >

<!-- 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>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<center>
<form>
Please enter a number with up to 2 decimal places: <br>
<input type=text name=numbox>
<input type=button name=ok value="Ok" onClick="checkDecimals(this.form.numbox, this.form.numbox.value)">
</form>
</center>

<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">JavaScript Source Code 3000</a></font>
</center><p>

<!-- Script Size: 1.42 KB -->










Wyszukiwarka

Podobne podstrony:
DecimalFormatSymbolsProvider
HTML AllowedAttributes
DecimalFormat
DecimalFormat
URI AllowedSchemes
DecimalFormatSymbols

więcej podobnych podstron