Page 1 of 1

Validation Not working please help

Posted: Sat Mar 12, 2011 9:04 pm
by karen2332
hi im newbie in php i have my registration i check the script triple times but it always submit even there is no password input the validation was stop in email txtbox this is my code. sorry for my bad english

Code: Select all

<?php
$LastName=$_POST['LastName'];
$FirstName=$_POST['FirstName'];
$MiddleName=$_POST['MiddleName'];
$eMail=$_POST['eMail'];
$PassWord1=$_POST['PassWord1'];
$PassWord2=$_POST['PassWord2'];
$Gender=$_POST['Gender'];
$Address=$_POST['Address'];
$BY=$_POST['BY'];
$BM=$_POST['BM'];
$BD=$_POST['BD'];
$CivilStatusID=$_POST['CivilStatusID'];
$ContactNo=$_POST['ContactNo'];
function check1($string) {
  if (preg_match('/[^a-z-A-Z-]/', $string)) {
      return false;
  } else {
      return true;
  }
}
function check2($string) {
	if(preg_match('/[^@.a-z-A-Z-0-9]/', $string)) {
		return false;
	} else {
		return true;
	}
}
function check3($string) {
if(preg_match('/[^0-9]/', $string)) {
	return false;
	} else {
		return true;
	}
}
 
if($LastName==""){
	?><script>history.go(-1);alert('Last name is required.');</script><?php
	echo $Age;
}
//check if is invalid string
if(ctype_digit($LastName)){
	?><script>history.go(-1);alert('Number Are not Allowed in LastName');</script><?php
}
//check if is invalid string
  if(!check1($LastName)){
	?><script>history.go(-1);alert('Number with Alphabetic Are not allowed in LastName..!!');</script><?php
	
}
if($FirstName==""){
	?><script>history.go(-1);alert('FirstName is required.');</script><?php
}
if(ctype_digit($FirstName)){
	?><script>history.go(-1);alert('Number not allowed in FirstName.');</script><?php
}
if(!check1($FirstName)){
	?><script>history.go(-1);alert('Number with Alphabetic are not Allowed in FirstName');</script><?php
	}
if($MiddleName==""){
	?><script>history.go(-1);alert('MiddleName is required.');</script><?php
}
if(ctype_digit($MiddleName)){
	?><script>history.go(-1);alert('Number not allowed in MiddleName.');</script><?php
}
if(!check1($MiddleName)){
	?><script>history.go(-1);alert('Number with Alphabetic are not Allowed in MiddleName')</script><?php
}
if($eMail==""){
	?><script>history.go(-1);alert('Email is required');</script><?php
}

if(check2($eMail)){
	?><script>history.go(-1);alert('Invalid Email Address');</script><?php
}
if(!check2($eMail)){
	?><script>history.go(-1);alert('Invalid Email Address')</script><?php
}
if($PassWord1==""){
	?><script>history.go(-1);alert('PassWord is required.');</script><?php
}
if(strlen($PassWord1) < 5){
	?><script>history.go(-1);alert('Password Should be Greater Than five')</script><?php
}
if(!$PassWord1==$PassWord2){
	?><script>history.go(-1);alert('Passwords not the same.');</script><?php
}
if($Address==""){
	?><script>history.go(-1);alert('Address is required.');</script><?php
}
if($ContactNo==""){
	?><script>history.go(-1);alert('Contact No is required.');</script><?php
}
if(ctype_alpha($ContactNo)){
	?><script>history.go(-1);alert('Alphabetic not Allowed in Contact Number');</script><?php
}
if(!check3($ContactNo)){
	?><script>history.go(-1);alert('Number with Alphabetic are not allowed in Contact Number');</script><?php
}
include 'InitDB.php';
$result=mysql_query("SELECT * FROM applicants WHERE eMail='$eMail'");
$num=mysql_num_rows($result);
if($num==0){
	if(mysql_query("INSERT INTO applicants(LastName,FirstName,MiddleName,eMail,PassWord,Gender,Address,dBirth,CivilStatusID,ContactNo,dApplied) VALUES('$LastName','$FirstName','$MiddleName','$eMail','$PassWord1','$Gender','$Address','$BY-$BM-$BD','$CivilStatusID','$ContactNo',Now())")){
		$result=mysql_query("SELECT * FROM applicants WHERE eMail='$eMail' AND PassWord='$PassWord1'");
		$num=mysql_num_rows($result);
		if($num==1){
			session_start();
			$_SESSION['ApplicantID']=mysql_result($result,0,'ApplicantID');
			header("location: ApplicantResume.php");
		}
		else{
			?><script>alert('Error creating applicant');</script><?php
		}
	}

	else{
		?><script>history.go(-1);alert('Error creating applicant');</script><?php
	}
}

else{
	?><script>history.go(-1);alert('eMail already registered.');</script><?php
}
?>

Re: Validation Not working please help

Posted: Sun Mar 13, 2011 10:23 pm
by stoneman30
We need to see the HTML form you are using to submit to this page. Are you sure you have your fields named correctly? If you enter values into every fields, does every field have a value on this post page? Post the HTML that goes along with this page, and it should be easier to diagnose the issue.

Re: Validation Not working please help

Posted: Mon Mar 14, 2011 11:48 am
by social_experiment

Code: Select all

if(check2($eMail)){
        ?><script>history.go(-1);alert('Invalid Email Address');</script><?php
}
if(!check2($eMail)){
        ?><script>history.go(-1);alert('Invalid Email Address')</script><?php
}
It looks like your logic is a bit messed up. The first check says if email is true, show an alert and your second check says if email is not true, also show an alert? Remove the first one imo.

Code: Select all

(!$PassWord1==$PassWord2)
// should be 
($PassWord1 != $PassWord2)

Re: Validation Not working please help

Posted: Tue Mar 15, 2011 12:06 pm
by karen2332
thank you for your reply stoneman and socialexperiment.
@socialexperiment thank you for your advice ill correct my code in email and password now it is work properly this is my revice script but the validation is always stop in email and still accepting an empty password.

Code: Select all

<?php
$LastName=$_POST['LastName'];
$FirstName=$_POST['FirstName'];
$MiddleName=$_POST['MiddleName'];
$eMail=$_POST['eMail'];
$PassWord1=$_POST['PassWord1'];
$PassWord2=$_POST['PassWord2'];
$Gender=$_POST['Gender'];
$Address=$_POST['Address'];
$BY=$_POST['BY'];
$BM=$_POST['BM'];
$BD=$_POST['BD'];
$CivilStatusID=$_POST['CivilStatusID'];
$ContactNo=$_POST['ContactNo'];
function check1($string) {
  if (preg_match('/[^a-z-A-Z-]/', $string)) {
      return false;
  } else {
      return true;
  }
}
function check2($string) {
	if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $string)) {
    	return false;  
   } else { 
      return true;
 }
} 
function check3($string) {
	if(preg_match('/[^0-9-]/',$string)) {
		return false;
	} else {
		return true;
 }
}
if($LastName==""){
	?><script>history.go(-1);alert('Last name is required.');</script><?php
}
if(strlen($LastName) < 3){
	?><script>history.go(-1);alert('Invalid Input in LastName Alphabet should be not less than 3');</script><?php
	} elseif (strlen($LastName) >16){
	?><script>history.go(-1);alert('Invalid Input in LastName Alphabet should be not more than 16');</script><?php
}	
//check if is invalid string
if(ctype_digit($LastName)){
	?><script>history.go(-1);alert('Number Are not Allowed in LastName');</script><?php
}
//check if is invalid string
  if(!check1($LastName)){
	?><script>history.go(-1);alert('Number with Alphabetic Are not allowed in LastName..!!');</script><?php	
}
if($FirstName==""){
	?><script>history.go(-1);alert('FirstName is required.');</script><?php
}
if(strlen($FirstName) < 3){
	?><script>history.go(-1);alert('Invalid input in FirstName Alphabetic should be not less than 3');</script><?php
	} elseif (strlen($FirstName) > 16){
	?><script>history.go(-1);alert('Invalid input in FirstName Alphabetic Should be not Greater Than 16');</script><?php
}
if(ctype_digit($FirstName)){
	?><script>history.go(-1);alert('Number not allowed in FirstName.');</script><?php
}
if(!check1($FirstName)){
	?><script>history.go(-1);alert('Number with Alphabetic are not Allowed in FirstName');</script><?php
	}
if($MiddleName==""){
	?><script>history.go(-1);alert('MiddleName is required.');</script><?php
}
if(strlen($MiddleName) < 3){
	?><script>history.go(-1);alert('Invalid input in MiddleName Alphabetic should be not less than 3');</script><?php
	}elseif(strlen($MiddleName) > 16){
	?><script>history.go(-1);alert('Invalid input in MiddleName Alphabetic should be not less than 16');</script><?php
}
if(ctype_digit($MiddleName)){
	?><script>history.go(-1);alert('Number not allowed in MiddleName.');</script><?php
}
if(!check1($MiddleName)){
	?><script>history.go(-1);alert('Number with Alphabetic are not Allowed in MiddleName')</script><?php
}
if($eMail==""){
	?><script>history.go(-1);alert('eMail is required.');</script><?php
}
if(!check2($eMail)){
	?><script>history.go(-1);alert('Invalid Email Address')</script><?php
}
if($PassWord1==""){
	?><script>history.go(-1);alert('PassWord is required.');</script><?php
}
if($PassWord1!=$PassWord2){
	?><script>history.go(-1);alert('Passwords not the same.');</script><?php
}
if(strlen($PassWord1) < 5){
	?><script>history.go(-1);alert('Password Should be Greater Than five')</script><?php
}
if($Address==""){
	?><script>history.go(-1);alert('Address is required.');</script><?php
}
if(ctype_digit($Address)){
	?><script>history.go(-1);alert('Invalid Address');</script><?php
}
if($ContactNo==""){
	?><script>history.go(-1);alert('Contact No is required.');</script><?php
}
if(ctype_alpha($ContactNo)){
	?><script>history.go(-1);alert('Alphabetic not Allowed in Contact Number');</script><?php
}
if(!check3($ContactNo)) {
	?><script>history.go(-1);alert('Number with Alphabetic are not allowed in Contact number');</script><?php
}
include 'InitDB.php';
$result=mysql_query("SELECT * FROM applicants WHERE eMail='$eMail'");
$num=mysql_num_rows($result);
if($num==0){
	if(mysql_query("INSERT INTO applicants(LastName,FirstName,MiddleName,eMail,PassWord,Gender,Address,dBirth,CivilStatusID,ContactNo,dApplied) VALUES('$LastName','$FirstName','$MiddleName','$eMail','$PassWord1','$Gender','$Address','$BY-$BM-$BD','$CivilStatusID','$ContactNo',Now())")){
		$result=mysql_query("SELECT * FROM applicants WHERE eMail='$eMail' AND PassWord='$PassWord1'");
		$num=mysql_num_rows($result);
		if($num==1){
			session_start();
			$_SESSION['ApplicantID']=mysql_result($result,0,'ApplicantID');
			header("location: ApplicantResume.php");
		}
	}
} else {
	?><script>history.go(-1);alert('eMail already registered.');</script><?php
}	
?>

Re: Validation Not working please help

Posted: Tue Mar 15, 2011 12:13 pm
by karen2332
stoneman30 wrote:We need to see the HTML form you are using to submit to this page. Are you sure you have your fields named correctly? If you enter values into every fields, does every field have a value on this post page? Post the HTML that goes along with this page, and it should be easier to diagnose the issue.
my fields are correct. this is my HTML form

Code: Select all

	<form action="ApplicantAdd.php" method="post" >
				  <table width="359" bgcolor="#9B8B4A">
					<tr><th>LastName</th><td><input name="LastName"/></td></tr>
					<tr><th>First Name</th><td><input name="FirstName"/></td></tr>
					<tr><th>Middle Name</th><td><input name="MiddleName"/></td></tr>
					<tr><th>eMail</th><td><input name="eMail"/></td></tr>
				<tr><th>Password</th><td><input type="password"  name="PassWord1"/></td></tr>
                           <tr><th>Confirm Password</th><td><input type="password"  name="PassWord2"/></td></tr>              <tr><th>Gender</th><td><select name="Gender"><option>Male</option <option>Female</option></select></td></tr>
					<tr><th>Address</th><td><input name="Address"/></td></tr>
					<tr><th>Birthday</th><td>
					<select name="BM"><?php
					for($x=1;$x<=12;$x++){
                                         ?><option value="<?php echo $x;?>" <?php if($x==date("m")){ ?>selected=""<?php }?>><?php echo date("M",strtotime("2000-$x-1"));?></option><?php
					}
					?></select>
					<select name="BD"><?php
					for($x=1;$x<=31;$x++){
				        ?><option value="<?php echo $x;?>" <?php if($x==date("d")){ ?>selected=""<?php }?>><?php echo $x;?></option><?php
					}
					?></select>
					<select name="BY"><?php
					for($x=date("Y");$x>1950;$x--){
					?><option value="<?php echo $x;?>"><?php echo $x;?></option><?php
					}
					?></select>
					</td></tr>
					<tr><th>Civil Status</th><td><select name="CivilStatusID"><option value="1">Single</option><option value="2">Married</option></select></td></tr>
					<tr><th>Contact No</th><td><input name="ContactNo"/></td></tr>
					<tr><th></th><td><input type="submit"  value="Submit"/></td></tr>
			</table>
		</form>

Re: Validation Not working please help

Posted: Thu Mar 17, 2011 11:22 am
by social_experiment

Code: Select all

<tr><th>LastName</th><td><input name="LastName"/></td></tr>
//
<input type="text" name="LastName" />
Your input fields are missing a type attribute, add those in, it might be what is causing your problems.

Re: Validation Not working please help

Posted: Fri Mar 18, 2011 3:13 am
by miki
there is some experience for coding.

1.get parameter
check the post data is necessary!
for example:

it's bad:

Code: Select all

$PassWord1=$_POST['PassWord1'];
it's good

Code: Select all

$PassWord1= isset($_POST['PassWord1'])?trim($_POST['PassWord1']):'';
because when u set error_reporting(E_ALL) (it's recommended when developing), if the post parameter is not exist, there is a notice. so check parameter first;

2.data type
if u want to compare a string with empty string. you should use "===" or strcmp() instead of "==".

3.test your code
the simplest way is write die("xxxx") in you code to test code, until the result is that you hope.

4.simple
why don't u define a function to intead of <script>alert("xxxxx")...

Code: Select all

function alert($msg)
{
    die("<script>history.go(-1);alert('{$msg}');</script>");
}

Re: Validation Not working please help

Posted: Wed Mar 23, 2011 4:21 pm
by karen2332
@miki thank you for your reply i correct my mistake in coding just i followed your instruction. thank you very much
this is my revice and finalize script

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=windows-1252" http-equiv="Content-Type" />
<title>Untitled 2</title>
<?php
$LastName = isset($_POST['LastName'])?trim($_POST['LastName']):'';
$FirstName = isset($_POST['FirstName'])?trim($_POST['FirstName']):'';
$MiddleName = isset($_POST['MiddleName'])?trim($_POST['MiddleName']):'';
$eMail = isset($_POST['eMail'])?trim($_POST['eMail']):'';
$PassWord1 = isset($_POST['PassWord1'])?trim($_POST['PassWord1']):'';
$PassWord2 = isset($_POST['PassWord2'])?trim($_POST['PassWord2']):'';
$Gender = isset($_POST['Gender'])?trim($_POST['Gender']):'';
$Address = isset($_POST['Address'])?trim($_POST['Address']):'';
$BY = isset($_POST['BY'])?trim($_POST['BY']):'';
$BM = isset($_POST['BM'])?trim($_POST['BM']):'';
$BD = isset($_POST['BD'])?trim($_POST['BD']):'';
$CivilStatusID = isset($_POST['CivilStatusID'])?trim($_POST['CivilStatusID']):'';
$ContactNo = isset($_POST['ContactNo'])?trim($_POST['ContactNo']):'';
$dob = $BY;
$age = date_diff(date_create($dob),date_create('now'))->y;

$msg = (' is required');
$msg1 = (' Alphabet should be not less than 3');
$msg2 = (' Alphabet should be not more than 16');
$msg3 = ('Number Are not Allowed in ');
$msg4 = ('Number with Alphabetic Are not allowed in ');
$msg5 = ('Invalid Email Address');
$msg6 = ('Passwords not the same');
$msg7 = ('Password Should be Greater Than five');
$msg8 = ('Invalid Address');
$msg9 = ('Alphabetic not Allowed in ');
$msg10 = ('Your Age not allowed to Register');
function check1($string) {
  if (preg_match('/[^a-z-A-Z-]/', $string)) {
      return false;
  } else {
      return true;
  }
}
function check2($string) {
	if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $string)) {
    	return false;  
   } else { 
      return true;
 }
} 
function check3($string) {
	if(preg_match('/[^0-9-]/',$string)) {
		return false;
	} else {
		return true;
 }
}
function alert($msg){
    die("<script>history.go(-1);alert('{$msg}');</script>");
	}
function alert1($msg1){
	die("<script>history.go(-1);alert('{$msg1}');</script>");
	}
function alert2($msg2){
	die("<script>history.go(-1);alert('{$msg2}');</script>");
	}
function alert3($msg3){
	die("<script>history.go(-1);alert('{$msg3}');</script>");
	}
function alert4($msg4){
	die("<script>history.go(-1);alert('{$msg4}');</script>");
	}	
function alert5($msg5){
	die("<script>history.go(-1);alert('{$msg5}');</script>");
	}
function alert6($msg6){
	die("<script>history.go(-1);alert('{$msg6}');</script>");	
	}
function alert7($msg7){
	die("<script>history.go(-1);alert('{$msg7}');</script>");
	}
function alert8($msg8){
	die("<script>history.go(-1);alert('{$msg8}');</script>");
	}
function alert9($msg9){
	die("<script>history.go(-1);alert('{$msg9}');</script>");
	}
function alert10($msg10){
	die("<script>history.go(-1);alert('{$msg10}');</script>");
	}
if($LastName===""){
	die("<script>history.go(-1);alert('LastName{$msg}');</script>");
}
if(strlen($LastName) < 3){
	die("<script>history.go(-1);alert('Invalid Input in LastName{$msg1}');</script>");	
	} elseif (strlen($LastName) > 16){
	die("<script>history.go(-1);alert('Invalid Input in LastName{$msg2}');</script>");
}	
//check if is invalid string
if(ctype_digit($LastName)){
	die("<script>history.go(-1);alert('{$msg3}LastName');</script>");
}
//check if is invalid string
  if(!check1($LastName)){
	die("<script>history.go(-1);alert('{$msg4}LastName');</script>");
}
if($FirstName===""){
	die("<script>history.go(-1);alert('FirstName{$msg}');</script>");
}
if(strlen($FirstName) < 3){
	die("<script>history.go(-1);alert('Invalid input in FirstName{$msg1}');</script>");
	} elseif (strlen($FirstName) > 16){
	die("<script>history.go(-1);alert('Invalid input in FirstName{$msg2}');</script>");	
}
if(ctype_digit($FirstName)){
	die("<script>history.go(-1);alert('{$msg3}FirstName');</script>");
}
if(!check1($FirstName)){
	die("<script>history.go(-1);alert('{$msg4}FirstName');</script>");
	}
if($MiddleName===""){
	die("<script>history.go(-1);alert('MiddleName{$msg}');</script>");
}
if(strlen($MiddleName) < 3){
	die("<script>history.go(-1);alert('Invalid input in MiddleName{$msg1}');</script>");
	}elseif(strlen($MiddleName) > 16){
	die("<script>history.go(-1);alert('Invalid input in MiddleName($msg2)');</script>");
}
if(ctype_digit($MiddleName)){
	die("<script>history.go(-1);alert('{$msg3}MiddleName');</script>");
}
if(!check1($MiddleName)){
	die("<script>history.go(-1);alert('{$msg4}MiddleName');</script>");	
}
if($eMail===""){
	die("<script>history.go(-1);alert('eMail{$msg}');</script>");
}
if(!check2($eMail)){
	die("<script>history.go(-1);alert('{$msg5}');</script>");
}
if($PassWord1===""){
	die("<script>history.go(-1);alert('PassWord{$msg}');</script>");
}
if($PassWord1!=$PassWord2){
	die("<script>history.go(-1);alert('{$msg6}');</script>");
}
if(strlen($PassWord1) < 5){
	die("<script>history.go(-1);alert('{$msg7}');</script>");
}
if($Address===""){
	die("<script>history.go(-1);alert('Address{$msg}');</script>");
}
if(ctype_digit($Address)){
	die("<script>history.go(-1);alert('{$msg8}');</script>");
}
if($age < 18){
	die("<script>history.go(-1);alert('{$msg10}');</script>");
}	
if($ContactNo===""){
	die("<script>history.go(-1);alert('ContactNo{$msg}');</script>");
}
if(ctype_alpha($ContactNo)){
	die("<script>history.go(-1);alert('{$msg9}ContactNo');</script>");
}
if(!check3($ContactNo)) {
	die("<script>history.go(-1);alert('{$msg4}in ContactNo');</script>");	
}

include 'InitDB.php';
$result=mysql_query("SELECT * FROM applicants WHERE eMail='$eMail'");
$num=mysql_num_rows($result);
if($num==0){
	if(mysql_query("INSERT INTO applicants(LastName,FirstName,MiddleName,eMail,PassWord,Gender,Address,dBirth,CivilStatusID,dApplied,ContactNo) VALUES('$LastName','$FirstName','$MiddleName','$eMail','$PassWord1','$Gender','$Address','$BY-$BM-$BD','$CivilStatusID',Now(),'$ContactNo')")){
		$result=mysql_query("SELECT * FROM applicants WHERE eMail='$eMail' AND PassWord='$PassWord1'");
		$num=mysql_num_rows($result);
		if($num==1){
			session_start();
			$_SESSION['ApplicantID']=mysql_result($result,0,'ApplicantID');
			header("location: ApplicantResume.php");
		}
	}
} else {
	?><script>history.go(-1);alert('eMail already registered.');</script><?php
}


?>

</head>

<body>

</body>

</html>