Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Mail validation form

the mail() function

Moderators: macek, egami, gesf

Mail validation form

Postby damaki » Mon Sep 12, 2011 1:12 am

I need to validate textform for mail, if entered mail is not in mail format, do not store into database. I have this code, but for some reasons doesn`t work. Please help me.

<?php
function check_email_address($email) {
// First, we check that there's one @ symbol,
// and that the lengths are right.
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters
// in one section or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
return false;
}
}
// Check if domain is IP. If not,
// it should be valid domain name
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
↪([A-Za-z0-9]+))$",
$domain_array[$i])) {
return false;
}
}
}
return true;
}
include('conn.php');
// Get values from form
$email = mysql_real_escape_string($_POST['email']);
$date = date("d.m.Y.");
if (!$email)
{
echo 'Molimo upišite Vaš e-mail';
exit;
}
if(!check_email_address($email)) {
echo 'E-mail nije validan';
exit;
// Insert data into mysql
$sql ="INSERT INTO newsletter (email,datum_upisa) VALUES ('$email', '$date')";
$result=mysql_query($sql) or die(mysql_error());
// if successfully insert data into database, displays message "Successful".
if($result) {
echo "Uspješno ste upisali Vaš e-mail.";
}
else {
echo "Greška pri upisu e-maila u bazu.";
}
// close connection
mysql_close();
?>
damaki
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Sep 12, 2011 1:08 am

Re: Mail validation form

Postby bigjoe11a » Tue Sep 27, 2011 8:40 pm

I thought I just did this for some one else.

Code: Select all
<?php

function check_email($email){
   
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {

return true;
} else {
return false;
}
} // ends function


$email = $_POST['email'];

if (check_email($email)) {
echo $email . ' is a valid email address.<br />';
} else {
echo $email . ' is not a valid email address.<br />';
echo "<br><a href=index.php>Return</a>";
exit;
}

?>
User avatar
bigjoe11a
New php-forum User
New php-forum User
 
Posts: 128
Joined: Wed Aug 31, 2011 3:55 am
Location: USA


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron