# PHP Form Validation

**URL:** <https://forum.kirupa.com/t/php-form-validation/265078>\
**Category:** programming\
**Created:** [July 3, 2008, 5:30pm UTC](https://forum.kirupa.com/t/php-form-validation/265078 "2008-07-03T17:30:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![atlnycdude24](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@atlnycdude24](https://forum.kirupa.com/u/atlnycdude24)\
**Post date:** [July 3, 2008, 5:30pm UTC](https://forum.kirupa.com/t/php-form-validation/265078/1 "2008-07-03T17:30:39Z")

</div>

\<?php if(isset($\_POST[‘submitted’])){ require\_once(’./database\_connect.php’); if (eregi (’^[[:alpha:].’ -] {2,60}$’, stripslashes(trim($\_POST[‘name’])))){ $n = escape\_data($_POST[‘name’]); } else { $n = FALSE; echo’\<p\>\<font color=“red” size="+1"\>Please enter your first name!\</font\>\</p\>’; } if(eregi (’^[[:alnum:]][a-z0-9_.-]\*@[a-z0-9.-]+.[a-z]{2,30}$’, stripslashes(trim($\_POST[‘email’])))){ $e = escape\_data($\_POST[‘email’]); } else { $e = FALSE; echo’\<p\>\<font color=“red” size="+1"\>Please enter your email!\</font\>\</p\>’; } if (eregi(’^[[:alnum:]]{6,10}$’, stripslashes(trim($\_POST[‘password1’])))){ if ($\_POST[‘password1’] == $\_POST[‘password2’]){ $p = escape\_data($\_POST[‘password1’]); } else { $p = FALSE; echo ‘\<p\>\<font color=“red” size="+1"\>Your password did not match the confirmed password!\</font\>\</p\>’; } } else { $p = FALSE; echo ‘\<p\>\<font color=“red” size="+1"\>Please enter a valid password!\</font\>\</p\>’; } if($n && $e && $p) { $query =“SELECT user\_id FROM users WHERE email=’$e’”; $result = mysql\_query ($query) or trigger\_error("Query: $query  
\<br /\>MySQL Error: " . mysql\_error()); if(mysql\_num\_rows($result) == 0){ $a = md5(uniqid(rand(), true)); $query = “INSERT INTO users (email, pass, name, active, registration\_date) VALUES (’$e’, SHA(’$p’), ‘$n’, ‘$a’, NOW())”; //IF PROBLEM WITH PASSWORD COME BACK HERE!! $result = mysql\_query ($query) or trigger\_error("Query: $query  
\<br /\>MySQL Error: " . mysql\_error()); if (mysql\_affected\_rows() == 1) { $body = "Thank you for signing up. In order to login into the admin panel, you must activate it by clicking on the following link:

“; $body .=“[http://admin.ksuwesely.org/activate.php?x=](http://admin.ksuwesely.org/activate.php?x=)” . mysql\_insert\_id() . “&y=$a”; mail($\_POST[‘email’], ‘Registration Confirmation’, $body); echo ‘\<h3\>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.\</h3\>’; include (’./includes/footer.html’); // Include the HTML footer. exit(); } else { // If it did not run OK. echo '\<p\>\<font color=“red” size=”+1"\>You could not be registered due to a system error. We apologize for any inconvenience.\</font\>\</p\>’; } } else { // The email address is not available. echo ‘\<p\>\<font color=“red” size="+1"\>That email address has already been registered. If you have forgotten your password, use the link to have your password sent to you.\</font\>\</p\>’; } } else { // If one of the data tests failed. echo ‘\<p\>\<font color=“red” size="+1"\>Please try again.\</font\>\</p\>’; } mysql\_close(); // Close the database connection. } // End of the main Submit conditional. ?\> My question is with form validation…It’s acting very weird.

[http://admin.ksuwesley.org/register.php](http://admin.ksuwesley.org/register.php)

everything validates except for the name. in other words even when i enter everything field the validation barks that I need to supply my name. What is going on??

---

<div class="post-metadata">

**Author:** ![atlnycdude24](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@atlnycdude24](https://forum.kirupa.com/u/atlnycdude24)\
**Post date:** [July 3, 2008, 5:32pm UTC](https://forum.kirupa.com/t/php-form-validation/265078/2 "2008-07-03T17:32:29Z")

</div>

sorry about the mess somehow it’s not letting me add the

```php

```

brackets so the code would be organized

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [July 3, 2008, 5:46pm UTC](https://forum.kirupa.com/t/php-form-validation/265078/3 "2008-07-03T17:46:30Z")

</div>

put your code in the [noparse]

```php

```

[/noparse] tags

---

<div class="post-metadata">

**Author:** ![atlnycdude24](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@atlnycdude24](https://forum.kirupa.com/u/atlnycdude24)\
**Post date:** [July 3, 2008, 5:53pm UTC](https://forum.kirupa.com/t/php-form-validation/265078/4 "2008-07-03T17:53:50Z")

</div>

```php
<?php             

if(isset($_POST['submitted']))
{ require_once('./database_connect.php');  

if (eregi ('^[[:alpha:]\.\' \-] {2,60}$', stripslashes(trim($_POST['name'])))){ $n = escape_data($_POST['name']); 

} else {

 $n = FALSE; echo'<p><font color="red" size="+1">Please enter your first name!</font></p>'; }  

if(eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,30}$', stripslashes(trim($_POST['email'])))){ 

$e = escape_data($_POST['email']); 

} else { 

$e = FALSE; echo'<p><font color="red" size="+1">Please enter your email!</font></p>'; }  

if (eregi('^[[:alnum:]]{6,10}$', stripslashes(trim($_POST['password1'])))){ if ($_POST['password1'] == $_POST['password2']){ $p = escape_data($_POST['password1']); 

} else {  

$p = FALSE; echo '<p><font color="red" size="+1">Your password did not match the confirmed password!</font></p>'; }  

} else {  

$p = FALSE; echo '<p><font color="red" size="+1">Please enter a valid password!</font></p>'; }  

if($n && $e && $p) {  

$query ="SELECT user_id FROM users WHERE email='$e'";
 $result = mysql_query ($query) or trigger_error("Query: $query
<br />MySQL Error: " . mysql_error()); 

 if(mysql_num_rows($result) == 0){ 
 $a = md5(uniqid(rand(), true));

  $query = "INSERT INTO users (email, pass, name, active, registration_date) VALUES ('$e', SHA('$p'), '$n', '$a', NOW())"; //IF PROBLEM WITH PASSWORD COME BACK HERE!! 

$result = mysql_query ($query) or trigger_error("Query: $query
<br />MySQL Error: " . mysql_error());  

if (mysql_affected_rows() == 1) {  

$body = "Thank you for signing up. In order to login into the admin panel, you must activate it by clicking on the following link:

"; 
$body .="http://admin.ksuwesely.org/activate.php?x=" . mysql_insert_id() . "&y=$a"; mail($_POST['email'], 'Registration Confirmation', $body);   

echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>';                

 include ('./includes/footer.html'); // Include the HTML footer.                
 exit();                                         
     } else { 
// If it did not run OK.                 

echo '<p><font color="red" size="+1">You could not be registered due to a system error. We apologize for any inconvenience.</font></p>'; } } else { // The email address is not available.           
  echo '<p><font color="red" size="+1">That email address has already been registered. If you have forgotten your password, use the link to have your password sent to you.</font></p>'; }             
 } else { // If one of the data tests failed.
         
echo '<p><font color="red" size="+1">Please try again.</font></p>'; } mysql_close(); // Close the database connection. 
 } ?>   

```

---

<div class="post-metadata">

**Author:** ![atlnycdude24](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@atlnycdude24](https://forum.kirupa.com/u/atlnycdude24)\
**Post date:** [July 3, 2008, 10:35pm UTC](https://forum.kirupa.com/t/php-form-validation/265078/5 "2008-07-03T22:35:30Z")

</div>

anyone?

---

<div class="post-metadata">

**Author:** ![agnus](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@agnus](https://forum.kirupa.com/u/agnus)\
**Post date:** [July 4, 2008, 9:28am UTC](https://forum.kirupa.com/t/php-form-validation/265078/6 "2008-07-04T09:28:52Z")

</div>

try this

```php

<?php
	$name = 'John J. Doe';
	echo preg_match("|^([\w\s\.?]+){1,5}$|Ui" , $name);
?>

```

It’s not fully tested so … use with caution. It’s just for example.  
Use preg\_match instead of ereg, it’s faster.
