Page 1 of 1
form validation
Posted: Wed Aug 25, 2004 12:57 pm
by robjime
I have a whole bunch of functions to validate various box's in my form. But I'm not quite sure how to apply those functions to the validation. For example i have function checkemail() which checks for a valid email address. But if the email address is not valid the script still submits all the information. How can i fix this?
Posted: Wed Aug 25, 2004 1:02 pm
by feyd
are these functions javascript? if this is being run through the onsubmit event hook, have the main function return false.
Posted: Wed Aug 25, 2004 1:07 pm
by robjime
no there php
Posted: Wed Aug 25, 2004 1:11 pm
by feyd
post your code.
Posted: Wed Aug 25, 2004 1:17 pm
by robjime
Code: Select all
//check password lenth, to check if both fields are the same, view register.php
function checkPassword($password) {
$length = strlen ($password);
if ($length < 6) {
return false;
echo 'Please select a password with 6 chars<br>';
}
else{
return True;;
}
}
//check password's
function checkpassword2() {
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
if($password == $password2){
return True;;
}
else {
return false;
echo 'Passwords did not match';
}
}
//check email
function checkEmail($email) {
$pattern = "/^[A-z0-9\._-]+"
. "@"
. "[A-z0-9][A-z0-9-]*"
. "(\.[A-z0-9_-]+)*"
. "\.([A-z]{2,6})$/";
if(preg_match ($pattern, $email)){
return True;;
}
else {
return false;
echo 'Please enter a valid e-mail address<br>';
}
}
//validate username
function newuser($username) {
include('config.php');
mysql_connect($dblocal,$user,$passwrd);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM users";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$dbuser=mysql_result($result,$i,"user");
$i++;
}
if(preg_match ("/$username/", $dbuser)) {
return false;
echo 'Username already taken<br>';
}
else {
return True;;
}
}
right know they return true and false, but when I said if(functions = true) { run success code } the success code ran and even though some functions were still false.
Posted: Wed Aug 25, 2004 1:34 pm
by feyd
FYI: code set after returns will not be run.
after the include, something like this:
Code: Select all
<?php
if(!checkPassword() || !checkPassword2() ..... )
die("\n\n\nerrors occurred.\n");
// your mysql connect and junk