# Contact Form Validation Help

**URL:** <https://www.phphelp.com/t/contact-form-validation-help/20282>\
**Category:** General PHP Help\
**Created:** [May 16, 2013, 2:40pm UTC](https://www.phphelp.com/t/contact-form-validation-help/20282 "2013-05-16T14:40:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://www.phphelp.com/uploads/default/original/2X/2/243e9ed5d93c5653ce3186f18df16e019ce38445.png) [@system](https://www.phphelp.com/u/system)\
**Post date:** [May 16, 2013, 2:40pm UTC](https://www.phphelp.com/t/contact-form-validation-help/20282/1 "2013-05-16T14:40:55Z")

</div>

Hi everybody,

I am trying to use PHP to validate a form on a contact page, but I keep getting valid input required errors although all the boxes have been completed.

This is my code:

[code]\<?php  
$to = ‘myemail@email.com’;  
$subject = ‘Subject Message’;  
$contact\_submitted = ‘Message displayed after form successfully validates.’;

```
      function email_is_valid($email) {
        return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
      }
      if (!email_is_valid($to)) {
        echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
      }
      if (isset($_POST['contact_submitted'])) {
        $return = "\r\n";
        $youremail = trim(htmlspecialchars($_POST['your_email']));
        $yourname = stripslashes(strip_tags($_POST['your_name']));
        $yoursubject = stripslashes(strip_tags($_POST['your_subject']));
        $yourmessage = stripslashes(strip_tags($_POST['your_message']));
        $contact_name = "Name: ".$yourname;
        $subject_text = "Subject: ".$yoursubject;
        $message_text = "Message: ".$yourmessage;
        $message = $contact_name . $return . $subject_text . $return . $message_text;
        $headers = "From: ".$youremail;
        if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yoursubject != "" && $yourmessage != "" ) {
          mail($to,$subject,$message,$headers);
          $yourname = '';
          $youremail = '';
          $yoursubject = '';
          $yourmessage = '';
          echo '<p style="color: blue;">'.$contact_submitted.'</p>';
        }
        else echo '<p style="color: red;">Please enter your name, a valid email address, a subject, and your message before sending your message.</p>';
      }
    ?>[/code]

```

And the related HTML portion:

[code]

Name

Email Address

Email Address

Message\<?php echo $yourmessage; ?\>

&nbsp;

[/code]

---

<div class="post-metadata">

**Author:** ![sebrenauld](https://www.phphelp.com/user_avatar/www.phphelp.com/sebrenauld/32/391_2.png) [@sebrenauld](https://www.phphelp.com/u/sebrenauld)\
**Post date:** [May 16, 2013, 2:56pm UTC](https://www.phphelp.com/t/contact-form-validation-help/20282/2 "2013-05-16T14:56:02Z")

</div>

[php]if (email\_is\_valid($youremail)  
&& !eregi("\r",$youremail)  
&& !eregi("\n",$youremail)  
&& $yourname != “”  
&& $yoursubject != “”  
&& $yourmessage != “” ) {[/php]

This is your code. First off, the checks #2 and #3 are pointless (the eregi calls). Your regular expression done through email\_is\_valid does not allow them, so they’ll never have a chance of getting there.

For the actual problem, look at what I’m going to highlight:

PHP code takes your\_message. That’s your problem.
