form validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

form validation

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

are these functions javascript? if this is being run through the onsubmit event hook, have the main function return false.
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

no there php
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post your code.
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
Post Reply