List:General Discussion« Previous MessageNext Message »
From:Christopher R. Jones Date:April 21 1999 7:01pm
Subject:Re: Is there a way to know if It's a number?
View as plain text  
>> ...
>> I am having problems at my website the people are sometimes entering
>> strings into fields that will be calculated with later on in MySQL
>> (currency units). I know there are other solutions to checking if a
>> value is a number (including JavaScript), but I do not want to use
>> another solution just for this.
>
>This is really a data validation problem and is best handled 
>BEFORE the data goes into the database. Presumably, you have 
>some sort of CGI script that accepts the data from the form, 
>possibly processes it and then puts it in the database. This 
>is probably the place to validate the data. If anything fails 
>the form can be represented...
>-- 
I agree, a database is supposed to be designed with integrity in mind,
thus, numbers should go into number fields.  I use Javascript to check for
valid numbers, the feed back to the user is instant and reliable.  An
example of a javascript function to validate numeric data (includes range
checking):

<HEAD>
<SCRIPT >
<!--
function rangeCheck( oInput1, nT_min, nT_max, cLabel )
{
	var lnInput = parseFloat(oInput1.value);
	var lnMax = parseFloat(nT_max);
	var lnMin = parseFloat(nT_min);
	
	if( isNaN( lnInput ) )
	{
		alert( cLabel + " Must Be A Valid Number" );
		//reset value to blank
		oInput1.value = "";
		oInput1.focus();
		oInput1.select();
	}
	else
	{
		if( lnInput < lnMin && lnMin!=999 )
		{
			alert( cLabel + " must be greater than " + nT_min );
			//reset value to blank
			oInput1.value = "";
			oInput1.focus();
			oInput1.select();
		}
		else
		{
			if( lnInput > lnMax && lnMin!=999 )
			{
				alert( cLabel + " must be less than " + nT_max );
				//reset value to blank
				oInput1.value = "";
				oInput1.focus();
				oInput1.select();
			}
		}
	}
}
//-->
</SCRIPT>
</HEAD>
<BODY>

<form NAME="form1">
		<input NAME="input1" HEIGHT="23" WIDTH="25" SIZE="5" VALUE="3.2"
			onchange="rangeCheck( this, '0.5', '6.5', 'Input 1 Error: ' )">
</form>

</BODY>


Christopher R. Jones, P.Eng.
14 Oneida Avenue
Toronto, Ontario M5J 2E3
Tel. 416 203-7465
Fax. 416 203-3044
Email cj@stripped


Thread
help! restoringpete collins19 Apr
  • Re: help! restoringChristian Mack20 Apr
    • Is there a way to know if It's a number?Jochen Haeberle20 Apr
      • Is there a way to know if It's a number?Michael Widenius21 Apr
        • Re: Is there a way to know if It's a number?Paul DuBois21 Apr
  • Re: Is there a way to know if It's a number?Benjamin Scherrey21 Apr
    • Re: Is there a way to know if It's a number?Thimble Smith21 Apr
  • Re: Is there a way to know if It's a number?Fred Read21 Apr
Re: Is there a way to know if It's a number?Christopher R. Jones21 Apr
  • Re: Is there a way to know if It's a number?Graham Ashton22 Apr