I am writing a validation function to help save me time when validating information commonly found in forms.
I am having a problem however in what my function is returning, which is supposed to be the result of the eregi function call. I read that the eregi function returns a 1 if a match is found or FALSE if no match was found.
My function is returning a 1 (as expected) for matches. HOWEVER it returns null (I am guessing) upon failure.
Here is my code with the results of each expression (at the bottom)
function validate($level,$value)
{
switch($level)
{
case "email":
RETURN eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',$value);
break;
case "address":
RETURN eregi('^[a-z0-9.,/]+',$value);
break;
case "textOnly":
RETURN eregi('[a-z]',$value);
break;
case "USphone":
RETURN eregi('[0-9]{10}',$value);
break;
case "USphoneDashed":
RETURN eregi('^[0-9]{3}-[0-9]{3}-[0-9]{4}$',$value);
break;
case "INTphone":
RETURN eregi('[0-9]+',$value);
break;
case "ssnPlain":
RETURN eregi('[0-9]{9}',$value);
break;
case "ssnDashed":
RETURN eregi('^([0-9]{3})-[0-9]{2}-([0-9]{4})$',$value);
break;
case "integer":
RETURN eregi('[0-9]+',$value);
break;
case "double":
RETURN eregi('^([0-9]*)\.([0-9]{0,2})',$value);
break;
case "noHTML":
RETURN eregi('[^<>\(\)\?;\\]',$value);
break;
case "USzip":
RETURN eregi('[0-9]{5,9}',$value);
break;
case "INTzip":
RETURN eregi('[a-z0-9]{3,15}',$value);
break;
}//end switch
}//end function
$successArray['email'] = "someone.names@city.rr.com";
$failArray['email'] = "@city.1";
$successArray['address'] = "123 Main Street";
$failArray['address'] = "edde1,fhy7";
$successArray['textOnly'] = "abc dfg nfhge";
$failArray['textOnly'] = "dkd dldlkdk dkd33 dld";
$successArray['USphone'] = "8504741234";
$failArray['USphone'] = "850457123";
$successArray['USphoneDashed'] = "850-123-1234";
$failArray['USphoneDashed'] = "8503240375";
$successArray['INTphone'] = "12123450909";
$failArray['INTphone'] = "123";
$successArray['ssnPlain'] = "123121234";
$failArray['ssnPlain'] = "121232";
$successArray['ssnDashed'] = "123-12-1234";
$failArray['ssnDashed'] = "123-121234";
$successArray['integer'] = "1234";
$failArray['integer'] = "12f123";
$successArray['double'] = "1115.23";
$failArray['double'] = "1115.235";
$successArray['noHTML'] = "input";
$failArray['noHTML'] = "<input>";
$successArray['USzip'] = "32507";
$failArray['USzip'] = "325077";
$successArray['INTzip'] = "L47F3";
$failArray['INTzip'] = "exit;123";
PRINT("<h3>VALIDATION TEST</h3><br>All of the following should be TRUE<br><br>");
FOREACH($successArray AS $index=>$value)
{
echo $value."....USING $index....".validate($index,$value)."<br>";
}//end foreach
PRINT("<h3>VALIDATION TEST</h3><br>All of the following should be FALSE<br><br>");
FOREACH($failArray AS $index=>$value)
{
echo "$value"."....USING $index....".validate($index,$value)."<br>";
}//end foreach
?>
And this is the result:
VALIDATION TEST
All of the following should be TRUE
[email]someone.names@city.rr.com....USIN[/email]G email....1
123 Main Street....USING address....1
abc dfg nfhge....USING textOnly....1
8504741234....USING USphone....1
850-123-1234....USING USphoneDashed....1
12123450909....USING INTphone....1
123121234....USING ssnPlain....1
123-12-1234....USING ssnDashed....1
1234....USING integer....1
1115.23....USING double....1
input....USING noHTML....1
32507....USING USzip....1
L47F3....USING INTzip....1
VALIDATION TEST
All of the following should be FALSE
@city.1....USING email....
edde1,fhy7....USING address....1
dkd dldlkdk dkd33 dld....USING textOnly....1
850457457....USING USphone....
8503240375....USING USphoneDashed....
123....USING INTphone....1
121232....USING ssnPlain....
123-121234....USING ssnDashed....
12f123....USING integer....1
1115.235....USING double....1
....USING noHTML....1
325077....USING USzip....1
exit;123....USING INTzip....1
And yes, some of the failures are passing - I need to tweak the regexs a bit.