This question already has answers here:
How to validate an Email in PHP?
(7 answers)
Closed 9 years ago.
i want to validate the email address domain using pregmatch. also the valid edu domain i inserted in email list array so when user enter the email address that entry first check in email list array. if it is available then it is validate. i am doing validation part on server side.. any help is appericiated. thanks in advanced...
<?php
$email = $_POST['email']; // get the email value
$email_exp = explode("#",$email); // split email
$email_name = $email_exp[1]; // get the domain of email address
$email_list = array("berkely.edu","ucfs.edu","udef.edu","ucms.edu","ucef.edu"); // valid edu domain
for($i=0;$i<sizeof($email_list);$i++)
{
if(in_array($email_name,$email_list))
{
if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_name))
{
// validate email
}
}
}
Use filter_var, and replace the preg_match call with it.
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == TRUE) {
// email is valid
}
So, the updated code will be:
<?php
$email = $_POST['email']; // get the email value
$email_exp = explode("#",$email); // split email
$email_name = $email_exp[1]; // get the domain of email address
$email_list = array("berkely.edu","ucfs.edu","udef.edu","ucms.edu","ucef.edu");
$email_is_valid = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == TRUE;
if($email_is_valid && in_array($email_name,$email_list) ) {
// email is valid for your purposes
}
Related
I'm trying to make the emails pass validation by using filter_var. However, I am not sure how to prevent the script from processing the form data to my database if the email is not valid.
I have
$email = $_POST['email'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
exit();
}
the email obviously comes from what was entered in by the user and is in the $_POST variable. The script DOES show the email as valid or invalid, however it STILL processes the script and places the form data into my database. I thought that putting "exit()" would be the solution to this, or the proper way to handle when it's not valid. It simply opens a new page where the echo print shows.
What am I missing or doing wrong? Ideally I would like the form field to highlight and give the user some indication that they've entered in an incorrectly formatted email address (although I know that is a different topic and somewhat a bells and whistles type of thing), but I certainly do not want to allow the script to process the data into my database.
The answer lies in where the validation code was placed. Instead of placing it RIGHT AFTER the posted variables and before the SQL insertion code, I put it at the very end of the script. So the posted data went into the database before they can be validated.
So now, I have (which works)
$name = $_POST['name'];
$email = $_POST['email'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is not a valid email address");
exit();
} else {
$msg_to_user = '<br /><br /><h4>Thanks ' . $name . ', we will send you news when appropriate. Take care!</font></h4>';
$name = "";
$email = "";
}
// THE SQL SELECT STATEMENT TO ENSURE NO DUPLICATE EMAIL AND THEN THE INSERT STATEMENT TO PUT THE DATA IN THE DATABASE COMES AFTER THE CODE ABOVE
This question already has answers here:
How to validate an email address in PHP
(15 answers)
Closed 7 years ago.
I have a form where I ask for information, but some (such as email) is not required. When the form for email is not empty, I'd like the email to be validated and sanitized. My current method only submits the data that is not empty. What's the most effective way to do the validation and sanitization?
Current method
if (empty($_POST["S_Email"])) {
$S_Email = "";
} else {
$S_Email = $_POST["S_Email"];
}
You're looking for PHP Validate Filters:
if (empty($_POST["S_Email"])) {
//No email address POSTed through
$S_Email = "";
} else {
//Email address POSTed
$S_Email = $_POST["S_Email"];
//Validate email
if(!filter_var($S_Email, FILTER_VALIDATE_EMAIL) === false){
//Email is valid
} else {
//Email is invalid
}
}
This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 9 years ago.
I'm using the WordPress API to create an options page. One of the inputs need to have an email entered. I need to write a function that will validate the email entered and return it.
function nl_validate_settings( $input ) {
if ( $field_args = array( 'type' => 'email' ) ) {
foreach( $input as $email ) {
if ( ! preg_match( '/^[A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-]+#[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+[A-Za-z]$/', $email ) ) {
$email = "Invalid email address!";
}
}
return $email;
}
}
This isn't working and I don't know what I am doing wrong. It doesn't save emails when an email is entered correctly.
I've checked other answers on StackOverFlow but couldn't find anything that would fix the problem. Your help would be appreciated!
I don't know how to format it
I would use filter_var and use a combination of both the Sanatize and Validation
$email = $_POST['email'];
$clean_email = filter_var($email, FILTER_SANITIZE_EMAIL);
if( filter_var($clean_email, FILTER_VALIDATE_EMAIL) )
{
//Case if Email is valid
}
else
{
//Handle the case the email is invalid
}
im getting the "invalid email address"
all is hardcoded for testing, what is missing? thanks!
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['example#example.com'];
$subject = $HTTP_POST_VARS['subjectaaa'];
$message = $HTTP_POST_VARS['messageeeee'];
/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}
/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>
Change
$email = $HTTP_POST_VARS['jaaanman2324#gmail.com'];
$subject = $HTTP_POST_VARS['subjectaaa'];
$message = $HTTP_POST_VARS['messageeeee'];
to
$email ='jaaanman2324#gmail.com';
$subject ='subjectaaa';
$message = 'messageeeee';
I think you want it to be hardcoded like this:
$email = 'jaaanman2324#gmail.com';
Otherwise you are trying to get the value out of HTTP_POST_VARS with the key of jaaanman2324#gmail.com
First, don't use $HTTP_POST_VARS, it's $_POST now.
Second, by writing $HTTP_POST_VARS['jaaanman2324#gmail.com'] you're looking for table element with juanman234#gmail.com key.
That's not what you wanted to do.
If you want to hardcode it, write
$email = 'jaaanman2324#gmail.com';`
if not, write
$email = $_POST['email'];
to get email field from form.
I'm making a new site, and already have email confirmation code in place. But is there a way of making a "go to your inbox" link that is specific to the users' email provider? For example:
Go to live.com if it's a #live.com email
Go to yahoo.com if it's a #yahoo.com email
Some sort of if statement I'm guessing? Just some ideas would be good!
$email = $_POST['email'];
$ext = array('live.com','yahoo.com');
$domain = explode('#', $email);
if ( $domain[1] == $ext[0])
{
// Go to Live.com
}
if( $domain[1] == $ext[1])
{
//Go to Yahoo.com
}
with email is field name in registration form /subscribe form