Validating email input in form using PHP - php

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.

Related

Form field Validation custom email requirements

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
}

Regex validate items with delimiter

I am using this regex for email validation in php (based on here)
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;
My question is how can I validate input that is a list of emails separated by a delimiter.
Let's say delimiter is "," or ";" or ", " or "; ".
I suppose i should add something like that
(\s*(;|,)\s*|\s*$)
but it doesn't work...
Validating an email for real is better done by a module than a short regex. See http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
But fortunately, php have a validator :
<?php
$email_a = 'joe#example.com';
$email_b = 'bogus';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "This (email_a) email address is considered valid.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This (email_b) email address is considered valid.";
}
?>
See http://php.net/manual/en/filter.examples.validation.php
Dont use regex to validate emails, PHP has a function for this filter_var():
$email = 'joe#example.com';
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
//valid
}else{
//not
}
You can adapt this code and use explode(',',$email) to validate multiple emails.
At the risk of giving you an answer you can't use if you're only accepting a pure regex solution, for a few reasons I would recommend using explode with your delimiter and proceed to iterate over the array and validate each email in turn:
Your regex and the code handling it will be simplified.
Your regex will only have to handle the general use case of emails and can be a general re-usable operation any time you need to validate an email address. It will also be simple to swap the regexp operation out for a call to a library meant for email validation, or any other custom validator.
It will be easier to handle possible related necessities, like indicating in your output which email failed to validate, or accepting all addresses which validated and discarding those that didn't.

regex assistance for validating an email address

I am trying to validate an email field. I took this regex from somewhere on here for and I used it on another form I made and it works fine. Yet when I use it now its not matching.
All I am trying to do is to check the email and if it is good then log it in the proper field in the db.
For the sake of not pasting a bunch of stuff... I have stripped out the problem lines and going to pseudo code next few lines.
Essentially, vars are these:
$theEmail = $_post email from first page here
$regEx ='#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+#([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si';
and my php is this
//essentially other field validation will go here...for now testing only empty.
if(!empty($theEmail)){
if (preg_match($regEx, $formEmail)) {
//send it through to db.
} else { //error stuff here }
}
essentially, this never comes true. The email never validates no matter what I do and as I said I wrote another more complicated form that validates data just fine
Not sure what is going on.
I would suggest you to use filter_var instead.
if (filter_var($theEmail, FILTER_VALIDATE_EMAIL)) {
//send it through to db.
} else {
//error stuff here
}
/^[a-z0-9.!\#$%&\'*+-=?^_{|}~]+#([0-9.]+|([^\s]+\.+[a-z]{2,6}))$/
I removed the first # and ending #si, and took out the / from the = since it was giving me problems. This generates a match on my e-mail address here:
<?
$theEmail = 'me#davebel.com';
$regEx ='/^[a-z0-9.!\#$%&\'*+-=?^_`{|}~]+#([0-9.]+|([^\s]+\.+[a-z]{2,6}))$/';
print_r(preg_match($regEx, $theEmail));
?>
Though this regex is very complex for something like e-mail validation- I would recommend trying to refine it and fine-tune it before putting it into production.
With email validation there are simple solutions that catch 99 % of all mistakes and complex solutions that might catch a tenth of a percent more, yet be unreadable.
Go the easy route and just check for something like
.+#.+\..+
Yes, it will allow an email address like a#b.c but that's probably a smaller price to pay than a user who cannot register because your 500-character regex has a mistake in it somewhere, rejecting a valid address.
give this a try! hopefully it will resolve your query, although there are infinte regulare expressions for email
^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$
For testing visit
Regular Expression Tester

How to validate non-english (UTF-8) encoded email address in Javascript and PHP?

Part of a website I am currently working on contains registration process where users have to provide their email address. Just recently I became aware that non-ascii based domains are possible (so is email).
My backend is utf-8 encoded MySQL where I am expecting any users (with differnt locales) should be able to enter their email but don't know how to validate this kind of email address.
Currently I am testing out jquery tools and it validates the english email address correctly but fails to validate non ascii email. Also I need to do same at server side with php. Is there a regular expression that can validate this kind of email address?
I have tried this but it fails in jquery tools (this is just example for demo, I don't understand this too)
闪闪发光#闪闪发光.com
Also what will happen when they type their English email address (jonesmith#somemail.com) with their own IME. Can this be validated with current regular expression we have for English mail validation. Currently I don't have to worry if that email exist for not.
Thanks
Attempting to validate email addresses may not be a good idea. The specifications (RFC5321, RFC5322) allow for so much flexibility that validating them with regular expressions is literally impossible, and validating with a function is a great deal of work. The result of this is that most email validation schemes end up rejecting a large number of valid email addresses, much to the inconvenience of the users. (By far the most common example of this is not allowing the + character.)
It is more likely that the user will (accidentally or deliberately) enter an incorrect email address than in an invalid one, so actually validating is a great deal of work for very little benefit, with possible costs if you do it incorrectly.
I would recommend that you just check for the presence of an # character on the client and then send a confirmation email to verify it; it's the most practical way to validate and it confirms that the address is correct as well.
Since 5.2 PHP has a build in validation for email addresses. But I'm not sure if it works for UFT-8 encoded strings:
echo filter_var($email, FILTER_VALIDATE_EMAIL);
In the original PHP source code you will find the reg exp for validating email, this can be used for manually validating when using PHP < 5.2.
Update
idn_to_ascii() can be used to "Convert domain name to IDNA ASCII form." Which then can be validated with filter_var($email, FILTER_VALIDATE_EMAIL);
// International domains
if (function_exists('idn_to_ascii') && strpos($email, '#') !== false) {
$parts = explode('#', $email);
$email = $parts[0].'#'.idn_to_ascii($parts[1]);
}
$is_valid = filter_var($email, FILTER_VALIDATE_EMAIL);
As offered by Mario, playing around a bit, I came up with the following regex to validate non-standard email address:
^([\p{L}\_\.\-\d]+)#([\p{L}\-\.\d]+)((\.(\p{L}){2,63})+)$
It would validate any proper email address with all kind of Unicode letters, with TLD limitations from 2 to 63 characters.
Please check it and let me know if there are any flaws.
Example Online
a reg exp could be something like this:
[^ ]+#[^ ]+\.[^ ]{2,6}
Got this idea from Javascript tutorial page. It is basic but it works for me without worrying about complexity of regular expressions and unicode standards.
Client side validation
if(!$.trim(value).length) {
return false;
}
else {
AtPos = value.indexOf("#");
StopPos = value.lastIndexOf(".");
if (AtPos == -1 || StopPos == -1) {
return false;
}
if (StopPos < AtPos) {
return false;
}
if (StopPos - AtPos == 1) {
return false;
}
return true;
}
Serverside validation
if(!isset($_POST['emailaddr']) || trim($_POST['emailaddr']) == "") {
//Error: Email required
}
else {
$atpos = strpos($_POST['emailaddr'],'#');
$stoppos = strpos($_POST['emailaddr'],'.');
if(($atpos === false) || ($stoppos === false)) {
//Error: invalid email
}
else {
if($stoppos < $atpos) {
//Error: invalid email
}
else {
if (($stoppos-$atpos) == 1) {
//Error: invalid email
}
}
}
Though it still has some loop holes, I guess users will not be fooling around with this stuff. Also real validation is requierd for serious stuff as suggested by 'Jeremy Banks'.
Hope this will be helpful for somebody else too.
Thanks and regards to all
On this subject I liked this page so much that I set up a blog exposing sites that do validation wrong (contributions gratefully received - don't let yours be on it!).
As far as using regexes go, those that say "it's wrong", tend to be light on alternatives, and TBH validation to the last letter of the RFC isn't really that critical - for example while noddy+!#$%&'*-/=?+_{}|~test#gmail.com is a perfectly valid address, it's not too unreasonable to reject it given that a surprisingly large proportion of users can't even type 'hotmail' correctly. Some domains are also quite restrictive on user names anyway, particularly hotmail. So I'm in favour of regexes that are demonstrably reasonable, and my favourite source for that is this page, though I don't like their current JS 'winner' and it would help if they set up a public test page.
jQuery's validate plugin uses this regex which is interestingly constructed, quite similar in style (but smaller!) to the ex-parrot one (actually my ISP!) linked by #powtac .
what is about something this:
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
mb_ereg('[\w]+#[\w]+\.com',$mail,'UTF-8');

How to check email address entered using php?

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

Categories