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

2004-04-07 Thread William Lovaton
Instead of doing a lot of casting, you can use is_numeric() to see if a
variable is a number or a numeric string.

-William

El mar, 06-04-2004 a las 14:19, John W. Holmes escribió:
 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...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Validating form field text input to be a specific variable type

2004-04-06 Thread Merritt, Dave
All,

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.  

if ( ($GLOBALS['PageOptions']['Type'] == 'Integer') and (!
is_int($GLOBALS['PageOptions']['Default']) ) )
{
// display an error
}

If I use (int) on the form default value then that won't work either because
if the default field value entered was not an integer but text such as
'snafu' then the value is always converted to an integer regardless.

if ( ($GLOBALS['PageOptions']['Type'] == 'Integer') and (! is_int((int)
$GLOBALS['PageOptions']['Default']) ) )
{
// display an error
}

Thanks in advance,

Dave Merritt
[EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2004-04-06 Thread John W. Holmes
From: Merritt, Dave [EMAIL PROTECTED]

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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Validating form field text input to be a specific variable type

2004-04-06 Thread Merritt, Dave
Okay seems to makes sense, but when I do the following it doesn't appear to
be working correctly, or I'm viewing my logic incorrectly one:

if ( (int)$PageOptions['Default'] == $PageOptions['Default'] )
{  
// display a message if the integer of the default value matches the
default value
}

Enter 5, message displayed, so correct
Enter 5.0, messaged displayed, incorrect
Enter 5.5, no message, so correct
Enter a, message displayed, incorrect
Enter 5.0a, message displayed, incorrect
Enter 5.5a, no message, so correct
Enter a5.5, message displayed, incorrect

What am I missing here?

Thanks,

Dave Merritt
[EMAIL PROTECTED]




-Original Message-
From: John W. Holmes [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, April 06, 2004 3:19 PM
To: Merritt, Dave; [EMAIL PROTECTED]
Subject: Re: [PHP] Validating form field text input to be a specific
variable type


From: Merritt, Dave [EMAIL PROTECTED]

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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Validating form field text input to be a specific variable type

2004-04-06 Thread Merritt, Dave
Okay seems to makes sense, but when I do the following it doesn't appear to
be working correctly, or I'm viewing my logic incorrectly one:

if ( (int)$PageOptions['Default'] == $PageOptions['Default'] ) {  
// display a message if the integer of the default value matches the
default value }

Enter 5, message displayed, so correct
Enter 5.0, messaged displayed, incorrect
Enter 5.5, no message, so correct
Enter a, message displayed, incorrect
Enter 5.0a, message displayed, incorrect
Enter 5.5a, no message, so correct
Enter a5.5, message displayed, incorrect

What am I missing here?

Thanks,

Dave Merritt
[EMAIL PROTECTED]




-Original Message-
From: John W. Holmes [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, April 06, 2004 3:19 PM
To: Merritt, Dave; [EMAIL PROTECTED]
Subject: Re: [PHP] Validating form field text input to be a specific
variable type


From: Merritt, Dave [EMAIL PROTECTED]

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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re[2]: [PHP] Validating form field text input to be a specific variable type

2004-04-06 Thread Richard Davey
Hello Dave,

Wednesday, April 7, 2004, 2:41:15 AM, you wrote:

MD Okay seems to makes sense, but when I do the following it doesn't appear to
MD be working correctly, or I'm viewing my logic incorrectly one:

MD if ( (int)$PageOptions['Default'] == $PageOptions['Default'] ) {  
MD // display a message if the integer of the default value matches the
MD default value }

MD Enter 5, message displayed, so correct
MD Enter 5.0, messaged displayed, incorrect
MD Enter 5.5, no message, so correct
MD Enter a, message displayed, incorrect
MD Enter 5.0a, message displayed, incorrect
MD Enter 5.5a, no message, so correct
MD Enter a5.5, message displayed, incorrect

MD What am I missing here?

When casting to an Integer don't forget it will loose any floating
point value, so 5.0 becomes 5 which according to above will display a
message. 5.0a will cast to 5, 5.5a should also cast to 5, but might be
rounding the wrong way? a5.5 will cast to 0.

-- 
Best regards,
 Richard Davey
 http://www.phpcommunity.org/wiki/296.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: RE: [PHP] Validating form field text input to be a specific variable type

2004-04-06 Thread holmes072000
 From: Merritt, Dave [EMAIL PROTECTED]
 
 Okay seems to makes sense, but when I do the following it doesn't appear to
 be working correctly, or I'm viewing my logic incorrectly one:
 
 if ( (int)$PageOptions['Default'] == $PageOptions['Default'] ) {  

This works:

if(strcmp((int)$PageOptions['Default'],$PageOptions['Default'])==0)

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re[3]: [PHP] Validating form field text input to be a specific variable type

2004-04-06 Thread Richard Davey
MD if ( (int)$PageOptions['Default'] == $PageOptions['Default'] ) {

Sorry, forgot to add that also bear in mind that the comparison
operator == will automatically cast your string to an integer for the
comparison. The following will take a user string ($str) and check if
it is an integer or not.

?
$str = 5.0a;

$v = intval($str);

if ($v === $str)
{
echo Integer;
}
else
{
echo Something else;
}
?

-- 
Best regards,
 Richard Davey
 http://www.phpcommunity.org/wiki/296.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php