0

After lots of trial and error I finally got this email validation up on wordpress http://info.cfgt.com.au/diploma-of-management/sq1 but the regex seems to only "half" work.

Basically it will allow anything@anthing and doesn't need the ".domain" at the end.

I've tried other regex strings but Wordpress seems to ignore any regex over about 30 characters, and the shorter ones aren't giving the goods.

<script>
jQuery(document).ready(function() {
jQuery('form').submit(function() {
email_address = jQuery('#inf_field_Email');           
email_regex = /^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.[a-z]{2,6}$/i;
if(!email_regex.test(email_address.val())){
alert('Please enter a valid email');
return false;
}else{
alert('All Good!');
return true;
}
});
});
</script>

Any ideas?

Cheers,

John Detlefs

  • It's 2012 and we still want to validate an email address using regular expressions. [This is a bad idea](http://stackoverflow.com/a/201378/417685) – Alexander Sep 03 '12 at 22:04
  • I'm open to other suggestions? – John Detlefs Sep 03 '12 at 22:21
  • The regex posted in your question should "work" (see the link in Alexander's comment for why regex and email validation don't go well together). But the reason it's not working on the live site is that the regex on line 181 has lost its escape characters. `email_regex = /^[a-z0-9._]*[a-z0-9_]@[a-z0-9][a-z0-9-.]*[a-z0-9].[a-z]{2,6}$/i;` The trailing `.[a-z]{2,6}` is no longer a literal period followed by 2-6 letters. Without the escape character it will match any character. – Bryan Sep 03 '12 at 22:25
  • Alexander, [not so bad](http://stackoverflow.com/a/4554294/815386) :) – zb' Sep 03 '12 at 22:26
  • @Bryan Yep, that's the ticket. Wordpress for some reason is stripping the \ from the code. I'll just put it into a js file and hopefully wordpress leaves it alone! Thanks for the help! Alexander: The reason I'm doing it with regex is that I have no control over the form really, the wordpress theme controls it, and on error the page is redirected to an ugly form that infusionsoft hosts and we get a lot of dropouts. So a quick little jquery validation solves the problem. – John Detlefs Sep 03 '12 at 22:38

1 Answers1

0

I think better to use very simple regexp in javascript for email like this:

/.*[a-90-1]+.*@.*[a-90-1]+.*\.[a-9]{2,6}/

and check that email is valid on backend (php or what you using)

zb'
  • 8,071
  • 4
  • 41
  • 68
  • You're probably right Eicto and i'll take a look at it. The main issue was Wordpress stripping the \ out of my code. Thanks for the help! – John Detlefs Sep 03 '12 at 22:39