0

I have a form that validates the email address and I want to be able to place echo '<p class="error">Please enter a valid email address!</p>'; anywhere on the web page without having to put the validation process within the html?

Or should I include the validation process in the HTML form?

Here is the php code.

if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) {
    $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email']));
} else {
    echo '<p class="error">Please enter a valid email address!</p>';
}
2
  • I don't have a clue what you are asking. You say want to echo {blah}, and you already do echo {blah}, so what do you need help with? Commented Mar 25, 2010 at 6:25
  • I want to be able to put <p class="error">Please enter a valid email address!</p> any where on the web page. Commented Mar 25, 2010 at 6:27

1 Answer 1

2

Slightly different

if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) { 
    $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email'])); 
} else { 
    $error = 'Please enter a valid email address!'; 
} 

Now you can print your $error anywhere on your page

Okay, make it this:

if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) { 
    $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email'])); 
    echo "valid ";
} else { 
    $error = 'Please enter a valid email address!'; 
    echo "invalid ";
} 
echo $error;
exit;

What would it say now?

Another example:

<?php
$error="";
if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) { 
  $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email'])); 
} else { 
  $error = 'Please enter a valid email address!'; 
  $email=htmlspecialchars($email);
} 
?>
<html>
<form>
<?php if ($error): ?>
  <p class="error"><?php echo $error?></p>
<?php endif ?>
Enter email: <input type="text" name="email" value="<?php echo $email?>">
<input type="submit">
</form>
</html>

Now it works?

Sign up to request clarification or add additional context in comments.

14 Comments

@GeNx: Then you just echo $error; wherever you want it to appear on your page. Did you do that?
@animuson of course I did that I'm not that much of a newbie :) I actually tried that before I posted this question.
@GeNx may be you just entered valid email? or you trying to print it on some other page? or just before validation?
@Col. Shrapnel, No I left out the @ sign so I know its not valid.
@GeNx never say "I know" but only "I have checked". Did you check what branch of your condition has worked? buy putting echo "valid"; and echo "invalid"; in the corresponding statements?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.