Field Validation for Number Field when User Enters Text (Error 500 Cannot convert text to number.)

Mindwatering Incorporated

Author: Tripp W Black

Created: 12/10/2003 at 09:55 AM

 

Category:
Notes Developer Tips
Fields

Situation:
If you have a number field and the user enters text in the field. When they submit, this field will produce an error 500. Unless the user has detailed error messages "on", which is off by default, in Internet Explorer, the user will not realize its them causing the error and think there is a server side issue.

Solution:
We need to test and stop the submit from occuring. Here are two possible approaches for handling this problem:
JS Fix Code:
This code needs to be put in the header.
Note: Cannot handle number punctuation and non-Integers.
<script language="JavaScript">
<!--
function isan(numtock) {
if (numtock.length == 0) {
return false;

}
for (var i=0;i < numtock.length;i++) {
if ((numtock.substring(i,i+1) < '0') || (numtock.substring(i,i+1) > '9')) {
return false;

}
}
return true;
}
//-->

</script>

Then add code to call this in the field's onchange event:
something like:
form=document.form[0];
if (isan(form.numberfieldname.value)==false) {
... do alert ... set field back to 0 ... etc.
}


JS Fix Code #2:
Following function handles integers and non whole numbers.
Note: Any text appended is trimmed -- only validates beginning numbers but can handle non whole decimal numbers.
function isnum(numtock) {
if (numtock.length ==0) {
return false;
}
if (isNanN( parseFloat(numtock, 10) )) {
return false;
}
}


JS Fix Code #3:
Following function handles integers and non whole numbers.
Note: Allows for US formatted decimal (0.00).
function isnum(numtock) {
var mychar;
var mypoint=0;
var isvalidnum;
// check for more than one decimal
for (var i=0; i < numtock.length; i++) {
mychar = numtock.charAt(i);
if (mychar ==".") {
mypoint ++;
}
}
if (mypoint>1) {
alert ('Invalid number: more than one decimal point.');
return false;
} else {
// check for non numbers
for (var i=0; i < numtock.length; i++) {
if ( numtock.charAt(i) !='.') {
if (isNaN( numtock.charAt(i) )) {
alert ('Invalid number: non numeric character entered as number!');
return false;
}
}
}
}
}
onChange or onBlur event for field should contain:
isnum(this.value)



Formula Fix:
Put this is the field input translation event:
tmp := @TextToNumber(@Text(fieldname));
@If(@IsError(tmp);0;tmp);


This will allow a submit to continue, but if the user put in garbage the value saved will be 0 instead of what number they may have entered with the garbage.
To workaround this, I would have the @IsError if replace the errror situation value with some number that the user is very unlikely to enter, say 999999. Then in the validation event just check for that value:
msg="sorry you must enter just a number in this field";
@If(fieldname=999999; @Failure(msg); @Success)

or perhaps:

tmpkill:=@Explode("a b c d e f g h i j k l m n o p q r s t u v w x y z / \ < > \ | ] [ ` ~ ! @ # % ^ & * ( ) : ;"; " ");
tmpleft1:=@ToNumber(@Trim(@ReplaceSubstring(@Text(@ThisValue); tmpkill; "") ));
tmpleft:=@If(@IsError(tmpleft1); 0; tmpleft1);
@If(@IsError(@ToNumber(@ThisValue)); tmpleft; @ThisValue)


previous page