0

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

user3204179
  • 69
  • 2
  • 2
  • 9
  • This is not a duplicate. I want to know how to format it – user3204179 Feb 18 '14 at 23:18
  • 1
    Then update your question. The word "format" does not currently appear in it. While you're at it, explain what you mean by "isn't working". – Blorgbeard Feb 18 '14 at 23:19
  • When you say you're using the "WordPress API", do you mean the Settings API (register_setting, do_settings_fields, etc.)? – Dave Ross Feb 18 '14 at 23:27
  • @Dave Ross Yes, I am. – user3204179 Feb 18 '14 at 23:29
  • I disagree with this answer linked above, even though it has been upvoted a thousand times. If you read in the comments, that regular expression will miss certain TLDs, (Top Level Domains) and does not allow the + character which is in the RFC spec for emails and has other flaws. Even the OP, @acrosman has acknowledged the problems with that Reg Ex and has stopped using it himself. – Newbi3 Feb 19 '14 at 06:56

1 Answers1

4

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
}
Newbi3
  • 356
  • 1
  • 8
  • And how would I do that? – user3204179 Feb 18 '14 at 23:24
  • You would have to use PHP version 5.3 and above to use this function. I would suggest using this built in php function over regular expressions as your email validation is only as good as the script you found and this function is battle tested for various emails – Newbi3 Feb 18 '14 at 23:28
  • Where does the $a come from? And why use $_POST? I am using the WordPress Api – user3204179 Feb 18 '14 at 23:30
  • Sorry typo. I meant $email. Although you asked a Wordpress specific question, I wanted to answer in general just incase someone else comes along and wonders how to use this later without wordpress. – Newbi3 Feb 18 '14 at 23:31
  • Here is also a W3Schools tutorial/article if anyone else needed help with this function [W3Schools PHP Filter](http://www.w3schools.com/php/php_filter.asp) – Newbi3 Feb 18 '14 at 23:37
  • Great (links)! Thanks! I'll try your answer and let you know if it works for me! – user3204179 Feb 19 '14 at 00:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47806/discussion-between-user3204179-and-newbi3) – user3204179 Feb 19 '14 at 00:58