# \[PHP\] Form validation

**URL:** <https://forum.kirupa.com/t/php-form-validation/212684>\
**Category:** programming\
**Created:** [January 13, 2007, 1:40am UTC](https://forum.kirupa.com/t/php-form-validation/212684 "2007-01-13T01:40:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![NeoDreamer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@NeoDreamer](https://forum.kirupa.com/u/NeoDreamer)\
**Post date:** [January 13, 2007, 1:40am UTC](https://forum.kirupa.com/t/php-form-validation/212684/1 "2007-01-13T01:40:55Z")

</div>

I want to warn users if the name and email field are invalid only if something has been entered. I used the code below. For some reason, the error message is _always_ shown even when I put in a _valid_ name and email.

```php

// validate whether name has both last and first name
if($name && !preg_match("$name", " "))
	$nameStatus1 = "<br />*Include both first and last name";
			
// validate email's form
if($email && (!preg_match("$email", "@") || !preg_match("$email", ".")))
	$emailStatus1 = "<br />*Invalid";

```

Also, another problem: everything but the file input can remember what was previously written in it. So when I need to display an error message, the page reloads, and the user’s previous file upload location is gone.

```auto

<input name=\"design\" value=\"$design\" type=\"file\" size=\"50\" maxlength=\"100\" />

```
