Re: Validating form field text input to be a specific variable type

From: Date: Tue, 06 Apr 2004 19:19:11 +0000
Subject: Re: Validating form field text input to be a specific variable type
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-182526@lists.php.net to get a copy of this message
From: "Merritt, Dave" <Dave.Merritt@ArvinMeritor.com> > I have a form on which the user is supposed to select a variable type > (boolean, integer, real, date/time, text) from a select box and enter the > default value for this selected variable type in a text box. I'm trying to > validate that the default value entered matches the variable type selected > i.e. user selects boolean so valid defaults could only be 0, 1, true, false > and anything else would generate an error, or the user selects integer and > enters 1.7 for the default would also throw a flag. I know that all of the > form values submitted from the web page are strings but is there a way to > test/convert the strings against the other variable types. > > I'm sure that I'm not explaining this very well, but for example, if I use > the following code I will always get an error displayed even if the user > enters a valid value such as 3 in the default field because the form values > are always submitted as strings. Well, if "(int)$string == $string", then the value is an integer. Same for "(float)$string == $string" for a real number. Boolean would be easy, just strtolower($string) as compare to 1, 0, 'true', or 'false'. Date/time validation will probaby require a regular expression or breaking it up to validate days/month, etc. That can get a little hairy. If they say "text", well, anything goes, right? Maybe just make sure it's not empty()? Let me know if you need more details. There are probably a ton of different ways to do this. ---John Holmes...

« previous php.general (#182526) next »