Jump to content

eregi is not displaying errors?


sayedsohail

Recommended Posts

Hi everyone,

 

the code below only display first occurance of false error message, infact i am sending both the email and password value in incorrect format of regexp.  So it should display both the errors when the functions are called.

 

Any help is greaty appreciated.

 

thanks,

 

<?php 
//declare variable 
$outdata=""; 

// Ist function 
function validate_email($email) 
{ 
$regexp = "^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$"; 

// validating the syntax of email 
if (!eregi($regexp, $email)) 
{  
global $outdata; 
$outdata.= "Email contains illegal characters, Please use a valid email address."; 
return false; 
} 

else  
{return true;} 
}  


//IInd function 

function validate_password($password) 
{ 
$regexp = "^([a-zA-Z0-9]{6,20})$"; 
         
// validating the syntax of password 
if (!eregi($regexp, $password)) 
{ 
global $outdata; 
$outdata.= "Password contains illegal characters, Please user letter, numbers only"; 
return false; 
} 
                 
else  
{return true;} 
} 


if (validate_email($email)  && validate_password($password)) 
{ 
//do something... 
} 

echo $outdata; 
?> 

Link to comment
Share on other sites

Ah, i've finally got it - this was really annoying me. Once the first function in your if statement returns false, php doesn't bother to evaluate the second part of it(and therefore the function does not run) - obviously it would be a waste of processing power since it has already found out that the if statement will overall be false.

 

What you'll have to do, is fun the two functions and store the output into variables:

<?php
$email_success = validate_email($email);
$password_success = validate_password($password);
if($email_success === true && $password_success === true){
//both true
}else{
//one or more false
}
?>

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.