Jump to content

[SOLVED] Simple form validation


NickG21

Recommended Posts

Hey everyone, below i have a very basic form with only three text boxes, i am looking to make sure only text is added in the first, only digits in the second, and valid e-mail format in the third.  the validations seem to work but i am unsure of how to run my if...elseif statements in order to make the form appear clean at first without displaying the error messages.
[code]<html>
<head>
</head>
<body>
<form action="testing1.php" method="post">
<input type="text" name="text1" value="Your Name"><br/>
<input type="text" name="text2" value="0"><br/>
<input type="test" name="email" value-"E-Mail Address"><br/>
<input type="submit" name="submit" value="submit"><br/>
</form>
<?php
$email = $_POST['email'];
$name = $_POST['text1'];
$number = $_POST['text2'];

// check e-mail address
// display success or failure message
if (!preg_match("/^([a-zA-Z])+/",$_POST['text1']))
{
echo("Text Only As Your Name, Please Re-Submit");
}
if (!preg_match("/^([0-9])+/",$_POST['text2']))
{
echo("Invalid Number Scheme");
}
if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email']))
{
echo("Invalid e-mail address");
}
echo "Valid e-mail address, processing...";

?>
</body>
</html>[/code]

any help is appreciated
thank you
Link to comment
Share on other sites

Try this:

[code]<html>
<head>
</head>
<body>
<form action="testing1.php" method="post">
<input type="text" name="text1" value="Your Name"><br/>
<input type="text" name="text2" value="0"><br/>
<input type="text" name="email" value-"E-Mail Address"><br/>
<input type="submit" name="submit" value="submit"><br/>
</form>
<?php
if (isset($_POST['submit'])){
  $email = $_POST['email'];
  $name = $_POST['text1'];
  $number = $_POST['text2'];

  // check e-mail address
  // display success or failure message
  if (!preg_match("/^([a-zA-Z])+/",$_POST['text1']))
  {
      echo("Text Only As Your Name, Please Re-Submit");
  }
  if (!preg_match("/^([0-9])+/",$_POST['text2']))
  {
      echo("Invalid Number Scheme");
  }
  if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email']))
  {
      echo("Invalid e-mail address");
  }
  echo "Valid e-mail address, processing...";
}
?>
</body>
</html>[/code]

Regards
Huggie
Link to comment
Share on other sites

I would personally recommend you do your processing first, and then display the error [b]above[/b] the form to assure that the user will see the error message. Try something like this:
[code]
<?php
if (isset($_POST['submit'])) {
  $error = array();
  if (!preg_match('|^[a-z]+$|i', $_POST['text1'])) $error[] = "First textfield may only contain letters";
  if (!preg_match('|^[0-9]+$|i', $_POST['text2'])) $error[] = "Second textfield may only contain numbers";
  if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $_POST['text3'])) $error[] = "Third textfield must contain a valid email address";
 
  if (count($error) > 0) {
    $msg = "<p class=\"error\">The following errors occurred:<br />\n" . implode('<br />\n', $error) . "</p>\n";
  } else {
    // Success!
  }
}

// Show your message if there is one
echo isset($msg) ? $msg : '';

// Now, display your form
?>
[/code]

Also, make note that your regular expressions did not have the terminating string '$' that is needed to assure that they contain [b]only[/b] those characters. As you have them currently checking, they only have to [b]start[/b] with one or more of the specified characters.

Hope this helps.
Link to comment
Share on other sites

obsidian, i agree that works much better and is much more manageable, however I receive this error after trying to put that code into mine
Warning: implode(): Bad arguments. in .../testing1.php on line 16
[code]<?php

if (isset($_POST['submit'])) {
$email = $_POST['email'];
$name = $_POST['text1'];
$number = $_POST['text2'];
$error = array();

// check e-mail address
// display success or failure message
if (!preg_match("/^([a-zA-Z])+/",$_POST['text1'])) $error[] = "Your Name May Only Contain Letters";
if (!preg_match("/^([0-9])+/",$_POST['text2'])) $error[] = "Invalid ZipCode, Please Re-Enter";
if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])) $error = "Invalid E-Mail Address";
if (count($error)>0)
{
$msg = "<p class=\"errpr\">The Following Errors Were Found:<br/>\n". implode('<br />\n', $error). "</p>\n";
}
else
{
echo isset($msg) ? $msg: 'Thank You, Your Information Has Been Accepted';
}
}
?>[/code]
Link to comment
Share on other sites

I believe it should be:
[code]
<?php
if (isset($_POST['submit'])) {
  $email = $_POST['email'];
  $name = $_POST['text1'];
  $number = $_POST['text2'];
  $error = array();

  // check e-mail address
  // display success or failure message
  if (!preg_match("/^([a-zA-Z])+/",$name)) $error[] = "Your Name May Only Contain Letters";
  if (!preg_match("/^([0-9])+/",$number)) $error[] = "Invalid ZipCode, Please Re-Enter";
  if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email)) $error[] = "Invalid E-Mail Address";

  if (count($error)>0) {
    $msg = "<p class=\"errpr\">The Following Errors Were Found:<br/>\n". implode('<br />\n', $error). "</p>\n";
  } else {
    $msg = "<p>Thank You, Your Information Has Been Accepted</p>";
  }
}

echo isset($msg) ? $msg : '';
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.