Jump to content

Registration form validation help needed


AdRock

Recommended Posts

I found an article here http://www.evolt.org/clever_php_forms that uses form validation

 

I have made some changes to meet my requirements, even though it's basically an outline but I have come across a few problems I don't know how to fix.

 

1st problem:

I have 2 fields; 1 to enter a password and the 2nd to match the password.  How do i match the passwords and give the errors message etc?

 

2nd problem:

1 of the fields is for an email address.  How do I check the email address and return an error message if not right?

 

3rd and 4th problem:

I would like to check the database to make sure the username and email address don't already exist in the database.

 

Here is the code I have so far

 

Register.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Form example</title>
<link rel="StyleSheet" href="styles.css" type="text/css">
</head>
<body>
<h1>Easyform Test</h1>
<div id="form">
<?PHP
include_once('easyform.php');
$errorindicator='<img src="alert.gif" width="20" height="20" alt="Alert" title="Indicator for missing form element" border="0" />';
$errorclass="error";
$Javascript=true;
$results=check();
if($results[0]=='Errors:'){
?>
<h2 class="errorhead">There has been an error:</h2>
<p>You forgot to enter the following field(s)</p>
<ul>
<?PHP foreach ($results as $i=>$e){if ($i>0){echo "<li>$e</li>";}}?>
</ul>
<?PHP }else if ($results['Name']!=''){ ?>
<h2 class="thankshead">Thank you for sending your data</h2>
<?PHP exit; }?>	

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="required" value="Name,SurName,Email,username,password1,password2,security" />
<fieldset>
	<legend>Your Credentials</legend>
		<label for="your_name">Name:</label>
			<?=add('<input type="text" name="Name" id="Your_First_Name" />')?><br />
		<label for="your_surname">Surname:</label>
			<?=add('<input type="text" name="SurName" id="Your_Surname" />')?> 
		<label for="your_email">Email:</label>
			<?=add('<input type="text" name="Email" id="Your_email_address" />')?>
		<label for="gender">Gender:</label>
			<?=add('<input type="radio" name="Gender" id="Male" checked="checked" value="Male" />')?>
			<label for="Male">Male</label>
			<?=add('<input type="radio" name="Gender" id="Female" value="Female" />')?>
			<label for="Female">Female</label>
</fieldset>
<fieldset>
	<legend>Login Credentials</legend>
		<label for="Your_username">Name:</label>
			<?=add('<input type="text" name="username" id="Username" />')?><br />
		<label for="your_password1">Enter password:</label>
			<?=add('<input type="password" name="password1" id="Password" />')?> 
		<label for="your_password2">Confirm password:</label>
			<?=add('<input type="password" name="password2" id="Confirm_password" />')?>	
</fieldset>
<fieldset>
	<legend>Anti-Spam key</legend>
		<label for="anti-spam">Anti-spam key:</label>
			<?=add('<input type="text" name="security" id="Anti-Spam_key" />')?><br />	
</fieldset>
<p align="right"><input type="submit" value="Send" /></p>
</form>
</div>
</body>
</html>

 

Here is the functions in easyform.php

<?PHP
/*
Easyform developer edition 
version 1.0 01.05.2003
written by Chris Heilmann
more info: http://www.onlinetools.org/articles/easyform/
*/
function check(){
if ($_POST!=''){
	$sentarray=array();
	$tocheck=explode(',',$_POST['required']);
	$error[0]="Errors:";
	foreach ($tocheck as $t){if(!array_key_exists($t,$_POST)){$error[]=$t;}}
	foreach (array_keys($_POST) as $p){
		if(!preg_match('/initvalue/',$p) and !preg_match('/required/',$p)){
			$sentarray[$p]=$_POST[$p]==$_POST[$p.'initvalue']?'':$_POST[$p];
		}
		foreach ($tocheck as $c){
			if ($p==$c and $sentarray[$p]==''){
				$error[]=$_POST[$p.'initvalue'];
			}
		}
	}
	return $error[1]==''?$sentarray:$error;	
	}
}

function add($f){
global $errorindicator,$errorclass,$Javascript;
$tocheck=explode(',',','.$_POST['required']);
preg_match('/id="(.*?)"/i',$f,$i);
preg_match('/name="(.*?)"/i',$f,$n);
preg_match('/type="(.*?)"/i',$f,$t);
preg_match('/value="(.*?)"/i',$f,$iv);
$n=$n[1];$iv=$iv[1];$i=str_replace('_',' ',$i[1]);
if(preg_match('/<textarea/',$f)){
	$v=$_POST[$n]==''?$i:$_POST[$n];
	$f=preg_replace('/<textarea(.*?)>(.*?)<\/textarea>/',
	'<textarea\\1>'.stripslashes(htmlentities($v)).'</textarea>',$f);
			if($Javascript){$f=preg_replace('/<textarea/','<textarea  onfocus="this.value=this.value==\''.$i.'\'?\'\':this.value"',$f);}
}	
if(preg_match('/<select/',$f)){
	preg_match('/<select.*?>/',$f,$st);
	preg_match_all('/<option value="(.*?)">(.*?)<\/option>/',$f,$allopt);
	foreach ($allopt[0] as $k=>$a){
		if($_POST[$n]==$allopt[1][$k] || ($_POST[$n]=='' && $k==0)){
			$preg='/<option value="';
			$preg.=$allopt[1][$k].'">'.$allopt[2][$k].'<\/option>/si';
			$rep='<option selected="selected" value="';
			$rep.=$allopt[1][$k].'">'.$allopt[2][$k].'</option>';
			$f=preg_replace($preg,$rep,$f);
		}
	}
}else{
	switch ($t[1]){
		case 'text':
			$v=$_POST[$n]==''?'value="'.$i.'"':'value="'.stripslashes(htmlentities($_POST[$n])).'"';
			$f=preg_replace('/<input/','<input '.$v,$f);
			if($Javascript){$f=preg_replace('/<input/','<input onfocus="this.value=this.value==\''.$i.'\'?\'\':this.value"',$f);}
		break;
		case 'checkbox':
			$v=$_POST[$n]=='on'?' checked="checked"':'';
			$f=preg_replace('/<input/','<input'.$v,$f);
		break;
		case 'radio':
			$f=$_POST[$n]==''?$f:preg_replace('/checked.*?\s/','',$f); 
			$v=$_POST[$n]==$iv?' checked="checked"':'';
			$f=preg_replace('/<input/','<input'.$v,$f);
		break;
	}
}
$f.='<input type="hidden" name="'.$n.'initvalue" value="'.$i.'" />';
if (array_search($n,$tocheck) and ($_POST[$n]=='' or $_POST[$n]==$i)){
		if($errorindicator!=''){$f=$errorindicator.$f;}
		if($errorclass!=''){$f=preg_replace('/name=/i','class="'.$errorclass.'" name=',$f);}
}
return $f;
}
?>

 

This is the code I use to check the email address whcih I use on my current page but i want to itegrate it into the new form

{
	$email = trim($email);
	$_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+";
	$_host = "([-0-9A-Z]+\.)+";
	$_tlds = "([0-9A-Z]){2,4}$/i";

	if( !preg_match($_name."@".$_host.$_tlds,$email))
	{
		$errmsg.="Email address has incorrect format!<br />";
		$valid=false;
	}
}

 

and here is how i compare passwords, check username, email address and if the captcha image matches what's entered in the text field

if( mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '$username'")) )
{
	$errmsg."The username you have selected has already been used by another member in our database. Please choose a different Username!<br />";
	$valid=false;
}					

if( mysql_num_rows(mysql_query("SELECT email_address FROM users WHERE email_address = '$email_address'")) )
{
	$errmsg.="Your email address has already been used by another member in our database. Please submit a different Email address!!<br />";
	$valid=false;
}

if ( strlen($password1) < 3 ){
	$errmsg.="Password must be more than 3 characters in legth<br />";
	$valid=false;
}					

if (strcmp( $password1,$password2 ) !=0){
	$errmsg.="Both passwords do not match<br />";
	$valid=false;
}

if (empty($_POST['verify']) && $_POST['verify'] == $_SESSION['captchstr'])
{
    $errmsg.="Please enter Anti-Spam key:<br />";
    $valid=false;
}

 

If anyone can help on any of this it would be a great improvement

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.