0

Possible Duplicate:
email address validation

<?php

require_once('includes/inc_auth2.php');
require_once('includes/config.php');
//require_once('auth3.php');


    if (isset($_POST['Submit'])) {


        $title=$_POST['title'];
        $forename = $_POST['forename']; 
        $surname=$_POST ['surname'];
        $dob=$_POST ['dob'];
        $gender=$_POST ['gender'];
        $email=$_POST['email']; 
        $phone=$_POST['phone'];
        $password=$_POST ['password'];


        if (authRegister($title, $forename, $surname, $dob, $gender, $email, $phone, $password))
        {
            echo 'Thank you for registering your details, you can now login'; 

            } 
            else
                {
                    outputErrors();
                    }



        if (empty($title))
            addError("Enter your title!",LEVEL_CUSTOMER);

        if (empty($forename))
            addError("Enter your name!",LEVEL_CUSTOMER);

        if (empty($surname))
            addError("Enter your surname!",LEVEL_CUSTOMER);

        if (empty($dob))
            addError("Enter your date of birth!",LEVEL_CUSTOMER);

        if ($gender == '0')
            addError("Enter your gender!",LEVEL_CUSTOMER);

        if (empty($email))
            addError("Enter your email!",LEVEL_CUSTOMER);

        if (empty($password))
            addError("Enter your password!",LEVEL_CUSTOMER);

}

?>


<?=outputErrors()?>
 <div id="form_style">

   <form name="Create_Account" method="post" action="?page=register">

        <label> Title: </label>
            <select name="title" id="title">
             <option value="0">Select Title</option>
<?
        #
            foreach($titlesArray as $key => $value){

    // if title is selected 
            if ($title == $key)
            $selected = true; 
            else
            $selected = false; 

            echo '<option value="'.$key.'"';

            if($selected) 
            echo ' selected="selected"';

            echo '>'.$value.'</option>';
        }
    ?>
    </select> 
 <br />


    <label>Forename:</label>
        <input name="forename" type="text" value="<?php if(isset($_POST['forename'])){  echo $forename; } ?>" /> <br />

    <label>Surname:</label>
        <input name="surname" type="text" value="<?php if(isset($_POST['surname'])){  echo $surname; } ?>" /> <br />

    <label>Email:</label>
        <input name="email" type="text" value="<?php if(isset($_POST['email'])){  echo $email; } ?>" /> <br />

            <label>phone:</label>
        <input name="phone" type="text" value="<?php if(isset($_POST['email'])){  echo $email; } ?>" /> <br />

    <label>Password:</label>
        <input name="password" type="password" value="<?php if(isset($_POST['password'])){  echo $password; } ?>" /> <br />

    <label>Confirm Password:</label>
        <input name="password" type="password" value="<?php if(isset($_POST['password'])){  echo $password; } ?>" /> <br />


    <label> Gender: </label>
        <select name="gender" id="gender">
        <option value="0">Select Gender</option>
        <?
        #
        foreach($gendersArray as $key => $value){

    // if gender is selected 
            if ($gender == $key)
            $selected = true; 
            else
            $selected = false; 

            echo '<option value="'.$key.'"';

            if($selected) 
            echo ' selected="selected"';

            echo '>'.$value.'</option>';
            }
            ?>
            </select> 

           <br />


           <label>D.O.B  : </label>
            <input name="dob" type="text" value="<?php if(isset($_POST['dob'])){  echo $dob; } ?>" /> <br/>

            <input name="Submit" type="submit" value="Register" class="button">
        </form>

    </div>
Community
  • 1
  • 1
Tawa
  • 1
  • 1
  • 1
  • 1

3 Answers3

3

To validate email addresses it is far better to use the native PHP filter functions and in particular the FILTER_VALIDATE_EMAIL (linked) validate filter.

For example:

if (empty($email) or !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    addError("Enter your email!",LEVEL_CUSTOMER);
}
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
0

Try this:

if(strpos($email, "@") === false) {
    addError("Email is invalid!", LEVEL_CUSTOMER);
}

For your other question, change the name of the confirm password box so that it is unique, like this:

<input type="password" name="password"...
<input type="password" name="password2"...

and then in your validation:

if($password != $password2) {
    addError("Passwords do not match!", LEVEL_CUSTOMER);
}

But passing your password field in plain text is not a good idea from a security standpoint.

Try validating your form using javascript before it is submitted, I made you an example here http://jsfiddle.net/dduncan/4GgTT/

Dai
  • 1,510
  • 1
  • 11
  • 12
  • Hi Dai, thatnk you very much for that, it worked perfect. How do i do the 'confirm password', i want the 'confirm password section to be able to see what has been entered in the original password field and match. If the password doesnt match, an error comes. Thanks – Tawa Mar 29 '11 at 10:40
  • @Tawa, I updated my answer for you, don't ask me for any info on security as that isn't my area at all. – Dai Mar 29 '11 at 15:31
0

You can give a simple validation for ex:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$

link for Other secure ways to validate email with more examples

Enjoy !

Aditya P Bhatt
  • 21,431
  • 18
  • 85
  • 104