form validation
Moderator: General Moderators
form validation
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?
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;;
}
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
FYI: code set after returns will not be run.
after the include, something like this:
after the include, something like this:
Code: Select all
<?php
if(!checkPassword() || !checkPassword2() ..... )
die("\n\n\nerrors occurred.\n");
// your mysql connect and junk