Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 554363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:42:06+00:00 2026-05-13T11:42:06+00:00

I am wondering if anyone out there can help with my form Validation Please?

  • 0

I am wondering if anyone out there can help with my form Validation Please?

I am having a few problems trying to synchronized out how certain bits of the actual structure of the script works together.

<?php
$flag="OK";   // This is the flag and we set it to OK
$msg="";        // Initializing the message to hold the error messages
   if(isset($_POST['Send'])){
      $key=substr($_SESSION['key'],0,4);
      $num_key = $_POST['num_key'];
      if($key!=num_key){
      $msg=$msg."Your Key not valid! Please try again!<BR>";
      $flag="NOTOK";
           }
      else{
    $msg=$msg."Your Key is valid!<BR>";
    $flag="OK";
        } 
         }
$email=$_POST['email'];
echo "Your Email: ".$email." is";
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
$msg=$msg."Invalid email<BR>";
$flag="NOTOK"; 
}else{
$msg=$msg."Valid Email<BR>";
$flag="OK";
}
$password=$_POST['password'];
if(strlen($password) < 5 ){ 
$msg=$msg."( Please enter password of more than 5 character length  )<BR>";
$flag="NOTOK"; 
}
if($flag <>"OK"){
echo "$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'>";
}else{ // all entries are correct and let us proceed with the database checking etc …
} 
function spamcheck($field)
  {
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
if (isset($_POST['email']))
  {//if "email" is filled out, proceed
  $mailcheck = spamcheck($_POST['email']); 
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
      }
?>

the problem, when email valid, password valid, though key is invalid the warning of key disappear, it mean passed too… and also the spamcheck doesn’t look work..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:42:06+00:00Added an answer on May 13, 2026 at 11:42 am

    You don’t have to set the flag to ‘OK’ or a previous error get masked, as you already noted.
    If all the check are ok, the flag remains in valid state and you can pass on, otherwise, if one of the check fails the flag reports the incorrect state.

      $flag="OK";   // This is the flag and we set it to OK
      $msg="";        // Initializing the message to hold the error messages
      if(isset($_POST['Send'])) {
        $key=substr($_SESSION['key'],0,4);
        $num_key = $_POST['num_key'];
        if($key!=$num_key){
        $msg=$msg."Your Key not valid! Please try again!<BR>";
        $flag="NOTOK";
      } else {
        $msg=$msg."Your Key is valid!<BR>";
      } 
    }
    
    $email=$_POST['email'];
    echo "Your Email: ".$email." is";
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
    $msg=$msg."Invalid email<BR>";
    $flag="NOTOK"; 
    }else{
      $msg=$msg."Valid Email<BR>";
    }
    
    $password=$_POST['password'];
    if(strlen($password) < 5 ){ 
      $msg=$msg."( Please enter password of more than 5 character length  )<BR>";
      $flag="NOTOK"; 
    }
    
    if($flag <>"OK"){
      echo "$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'>";
    } else { 
      // all entries are correct and let us proceed with the database checking etc …
    } 
    

    Said that I would use a different approach, for example using boolean values other than a string named flag. You can obtain a more fluent code calling it something like $inputIsvalid.

    Other nags: Sometimes you add the messages to a $msg variable, other you issue an echo, maybe it is an oversight.

    There is a lot of room for improvements, as every other code, I will address just some of the easy issues, for examples I will not check if the variables are set or not.

      $inputIsValid=true;   // This is the flag and we set it to OK
      $messages = array();        // Initializing the message to hold the error messages
    
      if(isset($_POST['Send'])) {
        $key=substr($_SESSION['key'],0,4);
        $num_key = $_POST['num_key'];
        if($key!=$num_key){
          $messages[]= 'Your Key not valid! Please try again!';
          $inputIsValid=false;
        } else {
          $messages[]'Your Key is valid!';
        } 
      }
    
      $email=$_POST['email'];
      $emailRegex='^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$';
      $emailIsValid = eregi($emailRegEx, $email);
      $messages[]= 'Your Email: '.$email.' is ' .($emailIsValid? 'Valid':'Invalid');
      $inputIsValid = $inputIsValid && emailIsValid;
    
      $password=$_POST['password'];
      if(strlen($password) < 5 ){ 
        $messages[]='( Please enter password of more than 5 character length  )';
        $inputIsValid=false; 
    }
    
    if(!inputIsValid){
      $messages[]='<input type='button' value='Retry' onClick='history.go(-1)'>';
      echo join('<br/>', $messages); 
    } else { 
      // all entries are correct and let us proceed with the database checking etc …
    } 
    

    Another approach should be (the functions are quite simple, but you can modify the validation policy of the different components without affecting the main code):

      function validateKey() {
        if(!isset($_POST['Send'])) {
          return true;
        }
    
        $key=substr($_SESSION['key'],0,4);
        $num_key = $_POST['num_key'];
        return $key==$num_key;
      }
    
      function validateEmail($email) {
        $emailRegex='^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$';
        return eregi($emailRegEx, $email);
      }
    
      function validatePassword($password) {
        return strlen($password) < 5;
      }
    
      $inputIsValid=true;   // This is the flag and we set it to OK
      $messages = array();        // Initializing the message to hold the error messages
    
      if(validateKey()) {
        $messages[]'Your Key is valid!';
      } else {
        $messages[]= 'Your Key not valid! Please try again!';
        $inputIsValid=false;
      }
    
      $emailIsValid = validateEmail($_POST['email']);
      $messages[]= 'Your Email: '.$email.' is ' .($emailIsValid? 'Valid':'Invalid');
      $inputIsValid = $inputIsValid && emailIsValid;
    
      $password=;
      if(!validatePassword($_POST['password']){ 
        $messages[]='( Please enter password of more than 5 character length  )';
        $inputIsValid=false; 
      }
    
    if(!inputIsValid){
      $messages[]='<input type='button' value='Retry' onClick='history.go(-1)'>';
      echo join('<br/>', $messages); 
    } else { 
      // all entries are correct and let us proceed with the database checking etc …
    } 
    

    Spam function:

    why are you using Constant different than the boolena values?
    (TRUE is different from true and FALSE is different from false)
    You can rewrite the function like this in order to obtain the desired behaviour.

    function spamcheck($field)
    {
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
      return filter_var($field, FILTER_VALIDATE_EMAIL);
    }
    
    if (isset($_POST['email'])) {//if "email" is filled out, proceed
      $mailcheck = spamcheck($_POST['email']); 
      if (!$mailcheck) {
        echo "Invalid input";
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck with recursion, was wondering if anyone can help me out with it.
I am wondering if anyone can help me out with parsing out data for
for a factoring program I'm making I'm wondering if anyone can help me out.
Does anyone know if there is a help authoring tool out there that can
Any astronomers out there? I'm wondering if anyone has produced or stumbled upon a
Wondering if anyone can help me with this annoying but trivial (in terms of
So hopefully with some CSLA skills out there can help me see a better
Just wondering if anyone knows if there are any MSBuild starter kits out there.
Wondering if anyone can help with this. I have a table with some fixed
Im wondering if someone could help me out with a little issue im having.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.