Jump to content

Registration form date validation help


PRodgers4284

Recommended Posts

I have a registration form to allow users to register to my website, i have validation for all the fields except the date field which is for entering a dob, i need the validation to check that the dob is in the format of 0000/00/00 else output an error "invalid dob", can anyone help me with some code to validate this field. My code so far is:

 

<?php
session_start(); 
include("database.php");
include("login.php");

//declare at top of page or in included file
function usernameTaken($username,&$conn){
    if(get_magic_quotes_gpc()){
        $username = stripslashes($username);
    }
    $username = mysql_real_escape_string($username);
    $q = "select username from users where username = '$username'";
    $result = mysql_query($q,$conn);
    return (mysql_num_rows($result) > 0);
}  

function emailTaken($email){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $email = addslashes($email);
   }
   $q = "select username from users where email = '$email'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}

function mobileTaken($mobile,&$conn){
    if(get_magic_quotes_gpc()){
        $mobile = stripslashes($mobile);
    }
    $mobile = mysql_real_escape_string($mobile);
    $q = "select mobile from users where mobile = '$mobile'";
    $result = mysql_query($q,$conn);
    return (mysql_num_rows($result) > 0);
}  


?>

<!--Register Form  --> 
<?php 
$error_stat = 0; 
$username_message = '';
$password_message = '';
$forename_message = '';
$surname_message = '';
$email_message = '';
$mobile_message = '';
$dob_message = '';



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

$username = $_POST['username']; 
$password1 = $_POST['password']; 
$password2 = $_POST['password2']; 
$md5password = md5($_POST['password']); 
$forename = $_POST['forename']; 
$surname = $_POST['surname']; 
$email = $_POST['email']; 
$mobile = $_POST['mobile']; 
$dob = $_POST['dob']; 
$location = $_POST['location']; 
$ip = $_SERVER['REMOTE_ADDR']; 


//Error checking 




//Username check) 
if (empty($username)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$username_message = 'Please enter a username.';
} 
if(usernameTaken($username,$conn))
{
    $error_stat = 1;
    $username_message = 'User name is taken please choose another one.';
}

$username = $_POST['username']; 
$username = trim($username); 

   if (strlen($username) > 12){ 
   $error_stat = 1; 
   $username_message = 'The username must be 12 characters or less'; 
}  





//Password check) 
if($password1 != $password2)
{
    $error_stat = 1;
    $password_message = 'Passwords don\'t match.';
}

if (empty($password1)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$password_message = 'Please enter a password.';
}

if(!$password1 || !$password2)
{
    $error_stat = 1;
    $password_message = 'Please enter both passwords';
}



//Forename check) 
if (empty($forename)) {
//Set the error_stat to 3, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$forename_message = 'Please enter your forename.';
}

else if (ctype_digit($forename)) { 
   $error_stat = 1; 
   $forename_message .= 'Invalid forename.'; 
} 

$forename = $_POST['forename']; 
$forename = trim($forename); 

   if (strlen($forename) > 12){ 
   $error_stat = 1; 
   $forename_message = 'The forename must be 12 characters or less'; 
}  




//Surname check) 
if (empty($surname)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$surname_message = 'Please enter your surname.';
}

else if (ctype_digit($surname)) { 
   $error_stat = 1; 
   $surname_message .= 'Invalid surname.'; 
}  

$surname = $_POST['surname']; 
$surname = trim($surname); 

   if (strlen($surname) > 12){ 
   $error_stat = 1; 
   $surname_message = 'The surname must be 12 characters or less'; 
} 





//Email check) 
if (empty($email)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter an email address
$email_message = 'Please enter your email address.';
}

//Check format of email address entered
else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)){
$error_stat = 1; 		 
//Set the message to tell the user to enter a valid email address
$email_message = 'Invalid Email Address.';
}

if(emailTaken($email,$conn))
{
    $error_stat = 1;
    $email_message = 'Email is taken please choose another one.';
}

$email = $_POST['email']; 
$email = trim($email); 

   if (strlen($email) > 30){ 
   $error_stat = 1; 
   $email_message = 'The email address must be 25 characters or less'; 
}  




//Mobile number check) 

if (empty($mobile)) {
//Set the error_stat to 5, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a dob
$mobile_message = 'Please enter your mobile number.';
}

else if (!ctype_digit($mobile)) { 
   $error_stat = 1; 
   $mobile_message .= 'The mobile phone number must be only numbers.'; 
}

if(mobileTaken($mobile,$conn))
{
    $error_stat = 1;
    $mobile_message = 'Mobile already in use, choose another one.';
}



$mobile = $_POST['mobile']; 
$mobile = trim($mobile); 

   if (strlen($mobile) > 11){ 
   $error_stat = 1; 
   $mobile_message = 'Invalid mobile number'; 
}  
  



//DOB check) 

if (empty($dob)) {
//Set the error_stat to 7, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a dob
$dob_message = 'Please enter your date of birth.';
}



$surname = mysql_real_escape_string($surname);


//Then, only run the query if there were no errors (if $error_stat still equals 0) 
if ($error_stat == 0) { 
   mysql_query("INSERT INTO users (username, password, forename, surname, email, mobile, dob, location, ipaddress) VALUES ('$username', '$md5password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location', '$ip')"); 
   echo "<h3>Registration Successful!</h3>"; 
   echo "<p>Thankyou, <b>$username</b>,registration was successful</p>"; 
   echo "<p>login.</p>";
  	echo "<a href=\"index.php\">Login</a>";
  	

}
}


//Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error 
if (!isset($_POST['submit']) || $error_stat == 1) { 


?> 


<!--this will show whatever is in the $message variable -->                    
<form method="post" class="registerform" action=""> 
<fieldset> 
<label for="username">Username:</label> 
<input name="username" type="text" id="username" value="<?php echo $_POST['username']; ?>" /> 
<?php echo "$username_message";?></fieldset> 

<fieldset> 
<label for="password">Password:</label> 
<input name="password" type="password" id="password" value="<?php echo $_POST['password']; ?>"/>  
<?php echo "$password_message";?></fieldset>

<fieldset style="width: 602; height: 29"> 
<label for="password">Re-type Password:</label> 
<input name="password2" type="password" id="password2" value="<?php echo $_POST['password2']; ?>" />  
</fieldset>  

<fieldset> 
<label for="forename">Forename:</label> 
<input name="forename" type="text" id="forename" value="<?php echo $_POST['forename']; ?>" /> 
<?php echo "$forename_message";?></fieldset> 

<fieldset> 
<label for="surname">Surname:</label> 
<input name="surname" type="text" id="surname" value="<?php echo $_POST['surname']; ?>" /> 
<?php echo "$surname_message";?></fieldset> 

<fieldset> 
<label for="email">Email:</label> 
<input name="email" type="text" id="email" maxlength="30" value="<?php echo $_POST['email']; ?>" /> 
<?php echo "$email_message";?></fieldset> 

<fieldset> 
<label for="mobile">Mobile:</label> 
<input name="mobile" type="text" id="mobile" value="<?php echo $_POST['mobile']; ?>"/> 
<?php echo "$mobile_message";?></fieldset> 

<fieldset> 
<label for="dob">DOB:</label> 
<input name="dob" type="text" id="dob" value="<?php echo $_POST['dob']; ?>"/> 
<?php echo "$dob_message";?></fieldset> 

<fieldset> 
<label for="location">Location:</label> 
<input name="location" type="text" id="location" value="<?php echo $_POST['location']; ?>" /> 
</fieldset> 
<p></p> 

<p style="text-align: center"> 

<input type="submit" name="submit" value="Register" /> 

</p> 

</form> 


<?php 
} 
?> 

 

Phil

Link to comment
Share on other sites

why not google "isdate() php"?? Bud there are functions made for nearly everything, just look it up on google.

 

simple way to put it....

 

grab date, explode() date, put date in an order for isdate() for example DD/MM/YYYY etc, if true carry on else error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.