PDA

View Full Version : Validation ----



Humper
11-30-2006, 09:41 PM
I cant get these two pieces of my validation to work...

if I have an IP of 192.168.1.1 -- it fails... it only passes if I have 192.168.111.100 -- a full IP ..... it needs to accept number like 10.1.1.1 etc...

#
# IP address
#
$field = $ipaddress;
$field_name = "<b>IP Address</b>";
if( (!$field)
|| strlen($field) < 8 || strlen($field) > 15
|| !preg_match("/[0-9]{3}+[.]{1}+[0-9]{3}+[.]{1}+[0-9]{3}+[.]{1}+[0-9]{3}/", $field, $matches )
)
{
$error_string .= $field_name . " cannot be empty and can only contain numbers 0-9 and periods (ex. 192.168.101.102).<br />";
}



this one just always fails...

#
# check phone
#
$field = $Phone;
$field_name = "<b>Phone Number</b>";
if( (!$field)
|| strlen($field) == 12 || strlen($field) > 12
|| !preg_match("/[0-9]{3}+[-]{1}+[0-9]{3}+[-]{1}+[0-9]{4}/", $field, $matches )
)
{
$error_string .= $field_name . " must be in 10 digit format (ex. 811-555-1212).<br />";
}


can anyone help me out..

boxxertrumps
11-30-2006, 10:21 PM
im not that good at SSI, but thats CGI, not PHP....


it appears that we only have 1 server side forum.
should be renamed to SSI...

mwinter
11-30-2006, 11:48 PM
if( (!$field)
|| strlen($field) < 8 || strlen($field) > 15
|| !preg_match("/[0-9]{3}+[.]{1}+[0-9]{3}+[.]{1}+[0-9]{3}+[.]{1}+[0-9]{3}/", $field, $matches )
)




$field = trim($field);
$octet = '(?2(?[0-4][0-9]|5[0-5])|(?1[0-9]|[1-9])?[0-9])';
$dottedOctet = "/^{$octet}(?\\.{$octet}){3}$/";

if (!$field || !preg_match($dottedOctet, $field, $matches)) {

That isn't necessarily sufficient: there are legal IP addresses that are reserved and shouldn't be encountered in some circumstances.





if( (!$field)
|| strlen($field) == 12 || strlen($field) > 12
|| !preg_match("/[0-9]{3}+[-]{1}+[0-9]{3}+[-]{1}+[0-9]{4}/", $field, $matches )
)




$field = trim($field);

if (!$field || !preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $field, $matches)) {





im not that good at SSI, but thats CGI, not PHP....

Those are three different things, and what the OP posted is PHP.

Mike

Humper
12-01-2006, 02:46 PM
The phone check works fine now....but the IP check still doesnt work..

if I enter 192.168.101.123 it fails


#
# check IP
#
#
$field = trim($ip_address);
$field_name = "<b>IP Address</b>";
$octet = '(?2(?[0-4][0-9]|5[0-5])|(?1[0-9]|[1-9])?[0-9])';
$dottedOctet = "/^{$octet}(?\\.{$octet}){3}$/";

if (!$field || !preg_match($dottedOctet, $field, $matches))
{
$error_string .= $field_name . " cannot be empty and can only contain numbers 0-9 and periods (ex. 192.168.101.123).<br />";
}


any one know why?

boxxertrumps
12-01-2006, 04:20 PM
[code]
Those are three different things, and what the OP posted is PHP.
Mike

sorry, used to seeing # in cgi scripts.. and aren't CGI & PHP serverside interpreter languages?

Twey
12-01-2006, 04:29 PM
CGI is not a language. It's a specification to pass various pieces of information to a program, which can then return a file.

# in SSI has a special meaning; in PHP, it's a single-line comment marker, as it should be in any language designed to be run with an interpreter. The C++-like double slash is preferred.

mwinter
12-01-2006, 07:31 PM
but the IP check still doesnt work..

Sorry. Four of the question marks should have been followed by a colon:



$octet = '(?:2(?:[0-4][0-9]|5[0-5])|(?:1[0-9]|[1-9])?[0-9])';
$dottedOctet = "/^{$octet}(?:\\.{$octet}){3}$/";

Mike

Humper
12-06-2006, 04:34 PM
this still doesnt allow 192.168.0.1 --- I still get the error code..

also on 192.168.100.101 -- I still get the error code..


any ideas?

Twey
12-06-2006, 05:58 PM
This is intentional. Those are both reserved IPs.

mwinter
12-09-2006, 10:24 PM
this still doesnt allow 192.168.0.1 --- I still get the error code..

also on 192.168.100.101 -- I still get the error code..

The updated regular expression I posted validates both of those as syntactically correct (and they are). However, as Twey wrote, those are part of the private address space and should not normally be expected. If this is for an intranet, fair enough, but the problem isn't with my suggestion.

Mike