PDA

View Full Version : php validation



308holes
January 31st, 2003, 18:14
Hello anyone have a Form Validation script in php ? that will validate the form befor its sent ?

Cagez
January 31st, 2003, 18:15
Depends what kind of information needs to be validated. Does it simply just need to check everything is filled in, does it need to check for valid email address, etc.

308holes
January 31st, 2003, 18:28
Ya just to check to see if thier blank and if blank alert the user to fill in the field(s)

Cagez
January 31st, 2003, 18:57
Look at the function empty (http://www.php.net/manual/en/function.empty.php) or isset (http://www.php.net/manual/en/function.isset.php). That should do the trick :)

308holes
January 31st, 2003, 19:08
Thows are good but how do i keep the form from submitting ?

Cagez
January 31st, 2003, 19:13
You have your HTML form that post's (the form method) the data to your PHP script. For example:


<?php
// We'll say that there's only 2 fields from the form that we posted to this script, the names of the fileds were "name" for their first name, and "lname" for their last name.


// get the values form the form
$name = $_POST['name'];
$lname = $_POST['lname'];

// below is read like "if name or lname (or both) is NOT set (hense the '!' at the beginning) then...
if(!(isset($name) || isset($lname)) {
echo "Please fill out the entire form.";
}

else {
// if everything is filled in, then the code here will be run
}
?>

Did that help?

CareBear
January 31st, 2003, 19:22
Originally posted by 308holes
Thows are good but how do i keep the form from submitting ? use "OnSubmit="return ValidateData();" clientside javascript and validate all the data in that function and return wether it's valid or not. If it's invalid then pop up a messagebox or something but the form won't be submitted.
You still need to validate the form afterwards in PHP though. Javascript can either be turned off or not supported by the browser and they could submit invalid data that way.

308holes
January 31st, 2003, 19:23
ya would empty be a better way ???

Cagez
January 31st, 2003, 19:24
CareBear's right. For simple validation like the one you want, it's probably easier to use Javascript so the user doesn't have to go to the next page, hit the back button etc. But then CareBear's right again, you'll still have to check it in PHP because some browsers may have Javascript turned off.

CareBear = Right :D

Cagez
January 31st, 2003, 19:25
Originally posted by 308holes
ya would empty be a better way ???
I dunno :D Never used empty before, probably better. Try it and let me know ;)

308holes
January 31st, 2003, 19:40
OK i did the isset got errors but the empty works Just Fine Thanks Alot!!

if(empty($Array["Name"]) || empty($Array["Time"])|| empty($Array["Date"]) || empty($Array["News"]))


WHat is a GOOD PHP IDE ?

Cagez
January 31st, 2003, 19:45
Check out NuSphere's PHPed (http://phped.com), its exspensive, but its supposed to be really good. You can download the trial though, I did. I "so-so" liked it.. Lests just say its uninstalled now ;)

spec
January 31st, 2003, 19:57
308 I will write you one. dont bother $$

308holes
January 31st, 2003, 20:01
Write me what an IDE ?? i got the validate script to work

Im just looking for a good PHP IDE Need some Color in my life ?

spec
January 31st, 2003, 20:06
<?
//error message maker
function error_message($msg) {
echo "<HTML><HEAD></HEAD><BODY>";
echo "<script>alert(\"error: $msg\");history.go(-1)</script>";
echo "</body></HTML>";
}

// the variables you are working with
$userid = $_GET['userid'];
$useremail = $_GET['useremail'];

//here is a sample checker for empty fields:

if(empty($userid)){
error_message(" Please Input a User Name");
exit();
}
//pattern for use with the email checker
$pattern ="^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw |az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|b y|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|c v|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi| fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs |gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir |is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li |lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|m q|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng| ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr |pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|s k|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|t o|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|v u|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$";

// see's if the email is in the format: user@domain.com

if(!eregi($pattern, $useremail)){
error_message("Your Email is not in the correct format: user@domain.com");
exit();
}
?>