Forum Moderators: open
I disagree! What makes you say that? JavaScript handles decimals just fine, as long as you know how to write the functions.
manjumurthy, should the decimal appear automatically after 9 digits?
Provided that the name of the form is "myForm", and the text field is "myField", this script should work cross-browser. If not, just replace with the actual names.
<script language="javascript" type="text/javascript">
function myFunc() {
theValue = document.myForm.myField.value;
rx = /[^0-9.]/;
if(rx.test(theValue)) {
alert("The field can only contain numbers");
return;
}
if(theValue.indexOf(".") != -1) {
theValue = theValue.substring(0,(theValue.indexOf(".") + 3));
}
lnt = theValue.length;
if(lnt > 11) {
if(theValue.indexOf(".") == -1) {
theValue = theValue.substring(0,11);
}
else {
theValue = theValue.substring(0,12);
}
lnt = theValue.length;
}
if(lnt > 9 && theValue.indexOf(".") == -1) {
first = theValue.substring(0,9);
second = theValue.substring(9);
theValue = first + "." + second;
}
document.myForm.myField.value = theValue;
}
</script>
<input type="text" maxlength="12" name="myField" onkeyup="myFunc()" />
Max 2 decimals
Max 11 digits (including decimals), plus period.
<form name="myForm">
<input type="text" maxlength="12" name="myField" onkeyup="myFunc()" onchange="NN4Func()" />
</form>