0

I'm using the following regex from emailregex.com to validate emails on my site. It's working like a charm in JS but I'm unable to get it working within PHP.

/^(([^<>()[]\.,;:\s@"]+(.[^<>()[]\.,;:\s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/

The issue from what I'm seeing is the backslashes, when adding it in quotes it keeps trying to escape it. Regex101 shows that it is working, it's just a matter of how to get it into PHP.

Any help would be great, thanks!

PHP Code:

$emailRegex = "/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/"
4
  • Post code from php script which not working Commented Mar 29, 2017 at 2:12
  • @diavolic See post, that var would be used in a preg_match() but the issue isn't with the match, it's that the string is trying to be escaped and I don't know how to make it so it's not being escaped. Commented Mar 29, 2017 at 2:16
  • If you cannot convert it at once - do it step by step, converting isolated parts one after another. Commented Mar 29, 2017 at 2:23
  • Test your regex in regex101.com Commented Jun 1, 2017 at 4:57

2 Answers 2

2

Use single quotes ' around regex text instead of double quotes "

  $emailRegex = '/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/';

Because you have used " in your regex,

Otherwise you need to escape " also by \"

Live demo : https://eval.in/763141

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

3 Comments

Warning: preg_match(): Unknown modifier '^'
Please provide any live demo like in eval.in
@Niklesh Thank you, it seems that coding for 3 days straight hasn't been very good on me. I missed that within my code I was trying to append onto the start and end of that regex / which was causing the error. Regardless you helped me out so thanks!
1

Don't use a regular exprssion.

Use PHP's filter_var functions:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email) email address is considered valid.\n";
}

Ref: http://php.net/manual/en/filter.examples.validation.php

You may choose to use a type="email" input on the client side. Disabling validation with the novalidate attribute on the form prevents the browser from changing the appearance of inputs based on the validity of their contents, but still allows you to use JavaScript to test.

var statusDiv = document.getElementById("status");
document.getElementById("email").addEventListener("change", function(evt) {
  statusDiv.textContent = this.checkValidity() ? "Valid" : "Not valid";
});
<form novalidate>
  <label for="email">Email</label>
  <input type="email" autocomplete="email" id="email">
</form>
<div id="status"></div>

3 Comments

Thought about this option and had it working but I want my backend validation to match my front-end validation. The regex works and doesn't seem to be showing any issues.
@JoeScotto - Are you using an input with a type="email" on the client side?
No, I want to control my validation from JS to keep it inline with my php validation. The messages from the email type mess with the whole design of my site also

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.