-2

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.

Dragan Marjanovic
  • 1,287
  • 5
  • 16
  • 27

7 Answers7

3

Use jquery validate really easy to use http://docs.jquery.com/Plugins/Validation

Jonathan Römer
  • 629
  • 5
  • 23
1

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!';
}
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
1

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.

fhugas
  • 392
  • 1
  • 10
1
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="";

            }
        }   
vivek salve
  • 991
  • 1
  • 9
  • 20
0

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

John
  • 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
0

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.

Dev
  • 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
0

Create your own captcha using php gd2 library. It's easy and efficient.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Dev
  • 186
  • 2
  • 12