Is there email validation baked in to Rails 3?

Php email validation help?

  • I am trying to edit an existing script that I have fund for a simple mailing list, now the list will post to a flat file but I need it to validate the address before and then if valid it will direct to the thankyou page. Here is the original code: <?php $email = $_POST['email']; $file = fopen("mailing.txt", "a"); fwrite($file, "\n" . $email); fclose($file); header("Location: hankyou.php"); ?> I had found a snippet for validation but I can't get it to work: <?php if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*… $email)) { echo "Valid email address."; } else { echo "Invalid email address."; } ?> I am trying to learn php but am in the early stages so help would be appreciated on this. If you can could you provide the correct code so I can see where I am going wrong. Thanks

  • Answer:

    Your code looks decent, but I'm not sure the pattern will work. Try this one instead: ^.+@.+\..{2,4}$ I'll break it down for you character by character... ^.+ one or more alphanumeric characters at the beginning @ an ampersand .+ (period plus) one or more characters following the ampersand \. (slash period) a literal period character .{2,4}$ two to four characters between the final period and the end This pattern matches all standard email addresses, but blocks these: andyaharrisbooks.net (no @) @aharrisbooks.net (no beginning term) andy@net (no period) andy@aharrisbooksnet (no period) [email protected] (incomplete domain) [email protected] (domain too long) You can check regular expressions with my tool here: http://www.aharrisbooks.net/xfd/book_4/chapter_6/regex.html (It uses JavaScript, but the idea is the same. I fully explain regular expressions using JavaScript in HTML / XHTML / CSS All in One for Dummies, and I explain the PHP usage in PHP6 / MySQL Programming for the Absolute Beginner. Please stop by my web site if you have any other questions or need more help. http://www.aharrisbooks.net

Cazpa at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

$email = $_POST['email'] if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a… $email)){ echo "<center>Invalid email</center>"; }else{ echo "<center>Valid Email</center>";}

Keith C

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.