views:

76

answers:

3

Hi, I was looking around and cannot seem to find a way to create a simple form with no submit button (enter only) that checks to make sure that the information typed in is really a email address. Any help would be appreciated.

*Disclaimer I know almost nothing about PHP. I realize this is very easy, I just do not understand it.

A: 

HTML forms accept hitting Enter as submitting the form by default, so you don't need to do anything extra.

For email validation, use the filter_var function with the FILTER_VALIDATE_EMAIL flag.

Example

HTML File:

<form method="post" action="yourfile.php">
    <input type="text" name="email" />
</form>

PHP File:

<?php
$email = $_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
    // Email is invalid, do something
}
else
{
   // Email is valid
}
?>

Note: do not use regex for parsing email addresses, per the reasons in this question - http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses

Zurahn
Or, with HTML 5, even easier....`<form action="blah" method="get"><input type="email" name="email" value=""></form>`
alpha123
A: 

A clarification the form is not php actually it is actually html, it may "go to" a php file.

To make the button not visible:

<input type="submit" style="visibility:hidden">

To validate email client side (without submitting - or in plain english going to the server and coming back):

Check out his tutorial:

http://tutorialblog.org/client-side-form-validation-with-jquery/

It is VERY easy!

If you actually wantend server side validation I would check out his tutorial:

http://www.w3schools.com/php/filter_validate_email.asp

I is also very very simple you don have to master any language.

Good luck

Trufa
A: 

A few points here:

1) The Username component of the Email Address does have a set of "Basic" rules

However, those rules are actually quite convoluted and complex. To create a Regular Expression which matched 100% of valid strings, and matched 0% of invalid strings is almost impossible.

2) The RFC states that the Only Server which should Validate the Username part of an Email Address is the host of that Domain

That is to say that an email to [email protected] should only have the "joe" component validated by the server handling email for "server.com".

3) Even an address which passes any Regular Expression test you may have may not be active/valid

I could say my email address is [email protected], which is probably a potentially valid address, but that does not mean that it exists, is active, or is mine.

http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx

A better solution for handling email addresses and checking their validity is to perform an extremely basic check using a regular expression (as simple as checking that the address contains at least one "@" symbol, and at least one "." symbol to the right of the right-most "@"), and then send an email to that address containing a link which would then post back through another form (using $_GET parameters) to advise that the message was successfully recieved through the specified email.

Example:

  1. User visits your form
  2. User enters all their data
  3. (Optional) Your form uses javascript to perform a quick check of the email address to ensure that the pattern /.+\@.+..+/ is present.
  4. User submits the form
  5. Your PHP form handler ("script") then rechecks the content of the form, including the email address where it uses preg_match() to check for /.+\@.+..+/
  6. Your PHP script saves the form content into a table, but marks that table as "pending"
  7. Your PHP script then generates an email message to the specified address containing a link to a PHP script on your site (ie http://www.server.com/validate.php?entry=1) and asking the user to click it to validate their submission
  8. The script at validate.php looks at it's $_GET['entry'] parameter and, if there is a row in your database table which matches that value, it marks it as "valid"

This is, admittedly, a very long and convoluted process to go through, but, if you want to be 100% sure that the email address the user submits is, at the very least, able to receive email at the time of the form submission, this is the only way it can be done.

It should also be noted that there are a number of services out there which allow users to setup temporary email addresses, plus normal email addresses are by no means permanent, so an address which validated today may not do so tomorrow.

Lucanos