Hello,

I have just stared learning about patterns, hoping to be able to write my own form validation script. I very new with php and was wondering if any of you could point out why my script doesn't work. It's two pages, test.html and process.php.

test.html

<form action="process.php" method="get">
  <p>First Name: 
    <input type="text" name="first_name" />
 </p>
     <p>Last Name: 
    <input type="text" name="last_name" />
</p>
  <p>Email: 
    <input type="text" name="email" />
</p>
  <p>Message: 
    <input type="text" name="message" />
    </p>
  <p>
    <input name="Submit" type="submit" value="Sumbit" />
        </p>
</form>

process.php

<php?
$first_name = $_GET["first_name"];
$last_name = $_GET["last_name"];
$email = $_GET["email"];
$message = $_GET["message"];


if (!preg_match("[A-Za-z]", $first_name));
{
	echo "\nFirst Name field was incorrect\n";
}

if (!preg_match("[A-Za-z]", $last_name));
{
	echo "\nLast Name field was incorrect\n";
}

if (!preg_match("/^[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/", $email));
{
	echo "\nEmailfield was incorrect\n";
}

if (!preg_match("[A-Za-z]", $message));
{
	echo "\nMessage field was incorrect";
}



?>

When I submit the information, I get this output no matter what I put into the text boxes, even if it matches the patterns.

First Name field was incorrect



Last Name field was incorrect



Emailfield was incorrect



Message field was incorrect

Anyone know what I'm doing wrong? I'd appreciate the help. :o

Later on I'll be using something like this to enter info into my db, but for now I'm just trying to figure out form validation.

    The problem is that you're asking for single characters.

    (and by the way, always wrap patterns in preg functions with ~~ or they won't work)

    for message (i assume you want to allow more than just one letter of a to z not to mention spaces and new lines

    '~(?s).*~'

    '~(?i)[a-z]+~' (for both first and last names)

      Thank you for the advice, I think I tried what you explained, but I'm still having the same problem ( still get the same output ). I've also tried 4 different patterns for the email preg_match(none worked), anything else you know of I could be doing wrong?

      Here is my process.php code now

      <php?
      $first_name = $_GET["first_name"];
      $last_name = $_GET["last_name"];
      $email = $_GET["email"];
      $message = $_GET["message"];
      
      
      if (!preg_match('~(?i)[a-z]+~', $first_name));
      {
          echo "\nFirst Name field was incorrect\n";
      }
      
      if (!preg_match('~(?i)[a-z]+~', $last_name));
      {
          echo "\nLast Name field was incorrect\n";
      }
      
      if (!preg_match("/^[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/", $email));
      {
          echo "\nEmailfield was incorrect\n";
      }
      
      if (!preg_match('~(?s).*~', $message));
      {
          echo "\nMessage field was incorrect";
      }
      
      ?>

      Also, what would you recommend for writing php scripts? So far I've done everything in dreamweaver wich doesn't provide much help. (doesn't underline syntax errors.. etc.)

        Note that the regexp's you've used for the letter fields will return true if there is at least one occurrence of any letter, regardless of what else is in the string. You need to either anchor the pattern:

        if(!preg_match('#^[a-zA-Z]+$#', $text))
        {
           // non alpha character found
        }
        

        Or else negate the character class:

        if(preg_match('#[^A-Za-z]#', $text))
        {
           // illegal character found
        }
        

        However, in these cases, you can save yourself some trouble and processing type by using the [man]ctype_alpha/man function:

        if(!ctype_alpha($text))
        {
           // illegal character
        }
        

        As far as your email address validation goes, it may give some false negatives. I'd recommend either this solution, or using the PHP [man]filter_var/man with its email validation constant.

        As far as editors, check out the "Echo Lounge" forum here for a number of threads on editors/IDE's.

          Write a Reply...