Page 1 of 1
email verification
Posted: Mon Jul 14, 2008 7:18 am
by pritam79
Hi there,
I have a form that validates user inputs for registration. My validation script has the following code to verify if the user has filled in a valid email address or not. This is the function.
Code: Select all
function valid_email($address)
{
// check email address is valid
if(ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $address))
return true;
else
return false;
}
But whenever the entered email address has a dot anywhere before the @ in the address, even if the address is correct i get a message which says 'not a valid email address'.
Code: Select all
if (!valid_email($email))
{
print("<script language = 'javascript'>alert('Not a valid email address');</script>");
exit();
}
Re: email verification
Posted: Mon Jul 14, 2008 7:30 am
by prometheuzz
pritam79 wrote:Hi there,
I have a form that validates user inputs for registration. My validation script has the following code to verify if the user has filled in a valid email address or not. This is the function.
Code: Select all
function valid_email($address)
{
// check email address is valid
if(ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $address))
return true;
else
return false;
}
But whenever the entered email address has a dot anywhere before the @ in the address, even if the address is correct i get a message which says 'not a valid email address'.
...
That is because everything before the '@' char must be in the following character class:
which does NOT contain a '.' (dot)
IMHO, you can better have a more "forgiving" e-mail validation: there is nothing more annoying if some silly form tells me that my e-mail address isn't valid while it surely is. I suggest something like this:
Code: Select all
"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"
Note that you don't need to escape the '.' (dot) inside a character class and also the '-' (hyphen) doesn't need escaping when you add it at the end or start of the character class.
Good luck.
Re: email verification
Posted: Mon Jul 14, 2008 10:50 am
by Dynamis
Good email verification regex's are pages long. I would recommend using php's built in e-mail validation. It will be much more accurate.
http://www.w3schools.com/php/filter_validate_email.asp
Re: email verification
Posted: Mon Jul 14, 2008 11:02 am
by prometheuzz
I didn't know PHP had it's own method for that! (I know very little PHP, just a bit of regex)
@OP: I agree with Dynamis, it is (almost) always a good idea to use built-in methods/functions: they have been tested thoroughly and are used a lot, so it is unlikely they contain bugs.
Re: email verification
Posted: Tue Jul 15, 2008 10:33 am
by pritam79
Thanks a lot prometheuzz, everything's working fine now. I have this one to ask again-
My registration form, gives error messages through alert boxes properly now on wrong input. But every time there is a wrong input, on submission of the form, i am directed to the "do_register.php" page along with the alert box. The new page says "Connection successful Could not execute query".
What i want is that on clicking the submit button, if there are any wrong inputs, i want only the alert box with the message but on the same "register.php page, so that i don't have to return back every time. Moreover, the database gets populated each time i click on submit.
This is "register.php"
Code: Select all
<?php
include "header1.php";
?>
<div id="content">
<div id="left" style="left: 0px; top: 0px">
<ul>
<li>Email address :</li>
<li>Preferred username :</li>
<li>Password :</li>
<li>Confirm password :</li>
</ul>
</div>
<div id="right">
<p style="left: -1px; top: 0px"><font size="4.5px">REGISTER</font></p>
<form action="do_register.php">
<input type="text" size="20" name="email"><br><br>
<input type="text" size="20" name="username"> (max 16 chars)<br><br>
<input type="password" size="20" name="passwd"> (6 to 16 chars)<br><br>
<input type="password" size="20" name="passwd2"><br><br>
<input type="submit" name="Submit" value="submit">
<input type="reset" name="Reset" value="reset">
</form>
</div>
</div>
<?php
include "footer.php";
?>
And this is "do_register.php"
Code: Select all
<?php
include "header1.php";
?>
<div id="content">
<?php
//include function files for this application
require("PDMS_fns.php");
// start session which may be needed later
// start it now because it must go before headers
session_start();
// email address not valid
if (!valid_email($email))
{
print("<script language = 'javascript'>alert('Not a valid email address');</script>");
exit();
}
// check username length
if(($username == "" ) || strlen($username)>16)
{
print("<script language = 'javascript'>alert('Username must be from 1 to 16 characters');</script>");
exit();
}
// passwords not same
if($passwd != $passwd2)
{
print("<script language = 'javascript'>alert('Passwords donot match');</script>");
exit();
}
// check password length is ok
// ok if username truncates, but passwords will get munged if they are too long.
if (strlen($passwd)<6 || strlen($passwd) >16)
{
print("<script language = 'javascript'>alert('Password must be between 6 to 16 characters');</script>");
exit();
}
// attempt to register
$reg_result = register($username, $email, $passwd);
if($reg_result == "true")
{
// register session variable
$valid_user = $username;
session_register("valid_user");
// provide link to members page
echo "You have been registered as $valid_user -go to members page and start";
}
else
{
// otherwise, provide link back, tell them to try again
echo $reg_result;
exit();
}
?>
</div>
<?php
include "footer.php";
?>
Re: email verification
Posted: Tue Jul 15, 2008 12:24 pm
by prometheuzz
pritam79 wrote:Thanks a lot prometheuzz, everything's working fine now. I have this one to ask again-
...
You're most welcome pritam. But unfortunately, I know very little PHP: the limited PHP I do know is how to use the regex related functions. That's why I never stray out of this section of the forum ; )
I suggest you post this follow up question in the general PHP programming section of this forum: without a doubt, someone is able to help you with this.
Best of luck!