I was looking for this answer, but I was not able to find this exact variation.
I'm looking for the following to validate:
#1) #toysrus.com
#2) #staples.com
#3) #example.com
And the following NOT to validate:
#1) staples.com
#2) Randy#staples.com
#3) #testcom
Basically, I want to force the user to enter a valid email domain, NOT an email address. Any clue how I could go about this?
I guess alternatively ask a user for their website domain name and just append the '#' character to that, but it's more confusing. It would be easier to simply to ask the user, please enter your company email domain name.
Thanks!
You can use the regular expression to match the email domain name.
For example:
$pattern = "^#(\w)+((\.\w+)+)$";
if (preg_match($pattern,$domain))
echo "Domain name right"
"^#" means the string should start with "#"."(\w)+" match at least 1 number or letter."(.\w+)+" match at least 1 domain name,such as ".com" or ".edu.cn". "$" means the end of the string.
This can be done in a multi-stage process
$email = 'something#somedomain.com';
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
// The email is a valid format so now we check the domain
$domain = explode('#', $email);
if(getmxrr($domain[1])) {
// This is about as far as we can take it.
// The email is valid and the domain has MX records
}
}
Related
Looking to create a form validation on email text field.
Have previously used validation to ensure correct email is produced.
But here looking to create a more custom rule which allows only emails ending in the format .ac.uk
Here a user would be able to provide any university/college/instituion as long as the last 6 characters in the string = .ac.uk with the general format for the mail as follows: email#university.ac.uk
Solution preferably in PHP, currently looking at employing a rule using the end part in this statement:
^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$
Making this part *(\.[a-z]{2,3}) relate to the .ac.uk
many thanks, much appreciated
Jeanclaude
I would first run the email through filter_var($email, FILTER_VALIDATE_EMAIL) rather than using a simple regex. It's not perfect (I've found a few edge cases that don't validate correctly) but it works well. Once you know it's a valid email address you can simply trust substr($email, -6) == '.ac.uk' and be done with it. Something like:
if (filter_var($email, FILTER_VALIDATE_EMAIL)
&& strtolower(substr(trim($email), -6))) === '.ac.uk') {
// Valid
}
I want only college students to be able to sign up my website, but I couldn't figure out how to control that. I also want .edu.fr, edu.tr or other .edu extensions to be able to join my website not just .edu's. I was thinking about using some reg-ex but I couldn't find any solution. I would be glad if someone can help me?
Shouldn't be that important but I am using PHP with laravel framework.
Most educational institutions have domain names that follow these pattern:
uni.edu
uni.edu.fr
uni.ac.uk
The following regular expression covers all such cases:
/(\.edu(\.[a-z]+)?|\.ac\.[a-z]+)$/
You can add cases to the regex as needed. Check that the email is real by sending an automated email with a confirmation link.
Corresponding PHP:
if (preg_match('/(\.edu(\.[a-zA-Z]+)?|\.ac\.[a-zA-Z]+)$/i', $domain)) {
// allow
}
There's not a great way to do it, but one possible way might be to explode the address using the # symbol:
// Split the email address into 2 values of an array using the # symbol as delimiter.
$emailParts = explode('#', $theEmailAddress);
// If the second part (domain part) contains .edu, period, country code or just .edu, then allow signup.
if (preg_match('/\.edu\.[^.]+$/i', trim($emailParts[1])) || preg_match('/\.edu$/i', trim($emailParts[1]))) {
// Use the above if you are assuming that the country codes can be any number of characters. If you know for sure country codes are 2 chars, use this condition:
// (preg_match('/\.edu\.[^.]{2}$/i', trim($emailParts[1])) || preg_match('/\.edu$/i', trim($emailParts[1])))
// Allow signup
}
Of course, this does NOT guarantee that the domain or the email address is an existing one!
I'm creating a form in PHP that contains a field called email where the user needs to enter his/her email ID. In order to ensure that the mail ID entered is authentic in terms of syntax (eg. username_123#domain.com is valid) I need to append some kind of validation to it. I find the situation quite nebulous as I don't understand how to check if the mail ID entered contains an # symbol etc. Kindly help. Thanks. :)
Best solution is to just do:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
...
}
and let PHP handle the heavy work for you. Otherwise, if you want to be strictly correct and use a regex directly yourself, you'll be stuck with this monstrosity:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
for strict RFC2822 compliance.
First you need to define valid e-mail.
There are different approaches to this depending on how important is this validation to you.
Some folks use crazy by-the-RFC regexps.
Another extreme is save anything user entered and later try sending confirmation e-mail to that address. No confirmation = bad e-mail.
You'll probably want something in between: make sure there's an # in the middle, for example:
$email_arr = explode('#', $email);
if (sizeof($email_arr) !== 2 || $email_arr[0] == '' || $email_arr[1] == '')
... // definitely not valid
UPD: Marc B nailed it with filter_var($email, FILTER_VALIDATE_EMAIL)
That's probably the best way.
You can use regex to validate the format:
<?php
$email = "someone#example.com"; // or perhaps $_POST['email'];
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
echo "Valid email address.";
}
else {
echo "Invalid email address.";
}
?>
http://php.net/manual/en/function.eregi.php
From my own code:
if( !preg_match( "(^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$)i", $email))
echo "E-mail address invalid";
A very small number of legitimate addresses may fail, such as anything #example.info, and any email address that uses unusual characters, but for the most part this works nicely.
I want to know that is there a way to know that the email id entered by the user is correct or not!
Like if the user enters email address shows Incorrect email entered! but if the user enters emailid#gmail.com shows Correct email entered!
Please help me out!
Thanks in advance!
Use preg_match function or ereg function and email regular expression
//$checkvalue contains your value to be matched
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $checkvalue))
{
echo "invalid email address";
}
else{
echo "valid email address";
}
You can check out this thread for a regex and function with a full explaination.
Use regular expression to check if the format of the email provided complies with a standard email format. If you don't know what regular expressions are, do a google search on them, they are a very powerful tool to have for any programmer. Just to get you started check out the preg_match method in PHP. Check out this link:
http://www.php.net/manual/en/function.preg-match.php
How can I check for duplicate email addresses in PHP, with the possibility of Gmail's automated labeler and punctuation in mind?
For example, I want these addressed to be detected as duplicates:
username#gmail.com
user.name#gmail.com
username+label#gmail.com
user.name+label#gmail.com
Despite what Daniel A. White claims: In Gmail, dots at random places before the '#' (and label) can be placed as much as you like. user.name#gmail.com and username#gmail.com are in fact the same user.
$email_parts = explode('#', $email);
// check if there is a "+" and return the string before
$before_plus = strstr($email_parts[0], '+', TRUE);
$before_at = $before_plus ? $before_plus : $email_parts[0];
// remove "."
$before_at = str_replace('.', '', $before_at);
$email_clean = $before_at.'#'.$email_parts[1];
Strip the address to the basic form before comparing. Make a function normalise() that will strip the label, then remove all dots. Then you can compare the addresses via:
normalise(address1) == normalise(address2)
If you have to do it very often, save the addresses in the normalised form too, so you don't have to convert them back too often.
This answer is an improvement on #powtac's answer. I needed this function to defeat multiple signups from same person using gmail.
if ( ! function_exists('normalize_email'))
{
/**
* to normalize emails to a base format, especially for gmail
* #param $email
* #return string
*/
function normalize_email($email) {
// ensure email is lowercase because of pending in_array check, and more...
$email = strtolower($email);
$parts = explode('#', $email);
// normalize gmail addresses
if (in_array($parts[1], ['gmail.com', 'googlemail.com'])) {
// check if there is a "+" and return the string before then remove "."
$before_plus = strstr($parts[0], '+', TRUE);
$before_at = str_replace('.', '', $before_plus ? $before_plus : $parts[0]);
// ensure only #gmail.com addresses are used
$email = $before_at.'#gmail.com';
}
return $email;
}
}
Perhaps this would be better titled "How to normalize gmail addresses in PHP, considering (user.name+label#gmail.com)"
You have two technical solutions above. I'll go a different route and ask why you're trying to do this. It doesn't feel right to me. Are you trying to prevent someone registering multiple times at your site using different e-mail addresses? This will only prevent a specialized case of that.
I have my own domain, example.com, and any e-mail that goes to any address at that domain goes to my single mailbox. Do you, now, want to put a check to normalize anything at my example.com to a single address on your end?
By the official e-mail address format, those addresses you are trying to match as the same are different.
Email address parsing is really, really hard to do correctly, without breaking things and annoying users..
First, I would question if you really need to do this? Why do you have multiple email addresses, with different sub-addresses?
If you are sure you need to do this, first read rfc0822, then modify this email address parsing regex to extract all parts of the email, and recombine them excluding the label..
Slightly more.. practically, the Email Address wikipedia page has a section on this part of the address format, Sub-addressing.
The code powtac posted looks like it should work - as long as you're not using it in an automated manner to delete accounts or anything, it should be fine.
Note that the "automated labeler" isn't a GMail specific feature, Gmail simply popularised it.. Other mail servers support this feature, some using + as the separator, others using -. If you are going to special-case spaces in GMail addresses, remember to consider the googlemail.com domain also
I have extended Zend Validator like this.
<?php
class My_Validate_EmailAddress extends Zend_Validate_EmailAddress
{
public function isValid($value)
{
$valid = parent::isValid($value);
if ($valid
&& in_array($this->_hostname, array('gmail.com', 'googlemail.com'))
&& substr_count($this->_localPart, '.') > 1) {
$this->_error(parent::INVALID_HOSTNAME);
$valid = false;
}
return valid;
}
}
Email with more than one "dot" symbol in gmail address are considered invalid. For some cases this is not logical solution, but that works for me.
function normalize($input) {
$input = str_replace('.', '', $input);
$pattern = '/\+(\w+)#/';
return preg_replace($pattern, '#', $input);
}