I'm working on a coming soon page. On the page I ask users to signup using their email through a form which then emails their emails to me. I'm not sure how to make sure that the data they are putting in the form are emails (to prevent spam). Any help? Thanks.
-
http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – Jonathan Payne Apr 28 '12 at 13:26
7 Answers
Use jquery validate really easy to use http://docs.jquery.com/Plugins/Validation

- 629
- 5
- 23
-
Thanks, I didn't take the jQuery motto (Write more do less) this seriously. Too easy. Thanks. – Dragan Marjanovic Apr 28 '12 at 13:30
Client side js is not enough, you should also validate with PHP's filter_var:
<?php
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$from=$_POST['email'];
}else{
$error['email_to']='Email Invalid!';
}
?>

- 46,049
- 7
- 62
- 106
-
Thanks Im using this just wondering in the above example the thing you have in the else statement, what does it do? – Dragan Marjanovic Apr 28 '12 at 13:52
-
Well if the email is not valid then you could echo the error `$error['email_to']` and it would tell the user the email is invalid – Lawrence Cherone Apr 28 '12 at 14:28
You shouldn't rely on client-side verification to keep spam and other unwanted registrations out. Although you could use jQuery validate to make the site more UI-friendly for the visitors, providing them with a neat message that the email address entered is not valid, and that they should review that field.
But a spam-bot will not care about that validation from jQuery, it will only post the fields given. That's why you need to validate the data on server-side.
There are several ways to validate email addresses with regex, but not all of them is reliable. Check out http://www.linuxjournal.com/article/9585 This article explains alot of this.

- 392
- 1
- 10
-
Thanks. Im relatively new to alot of this stuff. Really Helps. – Dragan Marjanovic Apr 28 '12 at 13:51
user following code as primary step to validate the email input
if(document.getElementById('Email').value=="")
{
document.getElementById('Email_Msg').innerHTML="Please Enter EmailId";
document.getElementById('Email').focus()
return false;
}
else
{
if(document.getElementById('Email').value.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/))
{
document.getElementById('Email_Msg').innerHTML="";
}
else
{
document.getElementById('Email_Msg').innerHTML="Enter Valid Email-Id";
return false;
document.getElementById('Email').value="";
}
}

- 991
- 1
- 9
- 20
If you want to prevent spam, making sure the email field is filled in with an email field is not enough alone. Consider using a captcha like ReCaptcha

- 529
- 3
- 7
-
I have considered this but it very ugly for a coming soon page. =( – Dragan Marjanovic Apr 28 '12 at 13:47
-
Fair point. An alternative is using a honeypot field in your form. Make it invisible using css, then check to make sure it wasn't filled in when it is submitted. If it was, it was most likely a spam bot, as they tend to fill in every field. A real user won't see the field - as it's invisible! Some extra info [here](http://nedbatchelder.com/text/stopbots.html) – John Apr 28 '12 at 16:39
-
Ah thanks that seems like a great idea. Never thought of that. – Dragan Marjanovic Apr 29 '12 at 01:45
just have a single line validation for your email. the code may be written like below.
<?php
if(empty($_POST['email']))
{
$error[]="Enter Email ID";
}
else
{
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
$error[]= "Not A Valid Email ID.";
}
}
//pass these errors to display ana error message to user.
foreach($error as $i)
{
$message = $i."<br />";
}
?>
use this code some where in your web page.
<?php
echo $message;
?>
Additionaly Use mail function and mysql database (or other if you are using) to send a mail to verify their email id. you can prevent unwanted users from registering. and also use captcha to prevent flooding to your database.
I think this helps. if any doubts reply here.

- 186
- 2
- 12
-
I plan on using cookies so the user can only submit the form once. =D and thanks it helps. But Im this is my first time using PHP, JQUERY, JAVASCRIPT, HTML, CSS altogether and I am also really new to PHP and JQUERY. So Im learning as I go. So far Ive managed fine but this email validation has been the hardest part. Thanks for the help. – Dragan Marjanovic Apr 28 '12 at 14:00
Create your own captcha using php gd2 library. It's easy and efficient.

- 90,663
- 31
- 146
- 203

- 186
- 2
- 12