I am working on a project and I am stuck on the registration page. I want to verify if:
- The mobile number already exists.
- The username already exists.
- The Email ID already exists.
Currently in my code I have added validation for the mobile number and it is working fine. But the username and email part I am not understanding how to implement it. Please help me out with my problem.
Here is my code.
<?php
$msg = '';
if(isset($_POST['register']))
{
$uname = (!empty($_POST['username']))?$_POST['username']:null;
$pass = (!empty($_POST['pass']))?$_POST['pass']:null;
$cpass = (!empty($_POST['cpass']))?$_POST['cpass']:null;
$fname = (!empty($_POST['fname']))?$_POST['fname']:null;
$lname = (!empty($_POST['lname']))?$_POST['lname']:null;
$email = (!empty($_POST['email']))?$_POST['email']:null;
$mobile = (!empty($_POST['mobile']))?$_POST['mobile']:null;
if($uname == '' || $pass == '' || $cpass == '' || $fname == '' || $lname == '' || $email == '' || $mobile == ''){
$msg = "<font color='red'>Fields cannot be empty</font>";
}else if(strlen($uname)<5){
$msg = "<font color='red'>Username must be at least 5 characters long</font>";
}else if(strlen($pass)<6 && strlen($cpass)<6){
$msg = "<font color='red'>Password must be at least 6 characters long</font>";
}else if($pass != $cpass){
$msg = "<font color='red'>Passwords are not matching</font>";
}else if(!is_numeric($mobile)){
$msg = "<font color='red'>Mobile number should contain only numbers</font>";
}else if(strlen($mobile)<10){
$msg = "<font color='red'>Mobile number should be at least 10 characters long</font>";
}else{
$query = "SELECT user_mobile FROM user_reg WHERE user_mobile = '".$mobile."'";
$query1 = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($query1);
$row = mysql_fetch_array($query1);
if($num_rows > 0)
{
$msg = "<font color='red'>Mobile number already exists. Please try again...</font>";
}
else{
$str = "INSERT INTO user_reg(user_email, user_uname, user_pass, user_fname, user_lname, user_mobile)VALUES('$email','$uname','$pass','$fname','$lname','$mobile')";
$sql = mysql_query($str) or die(mysql_error());
if($sql){
$msg = "<font color='green'>Regstration successfull. Please Login to use your account.</font>";
}else{
$msg = "<font color='red'>Sorry.. There are some errors. Please fix them before you continue.</font>";
}
}
}
}
?>
HTML part.
<div class="reg-box"><br />
<center>
<?php echo $msg; ?>
</center>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<label>Username</label>
<input type="text" name="username" value="" class="a-text" />
</div>
<div>
<label>Password</label>
<input type="password" name="pass" value="" class="a-text" />
</div>
<div>
<label>Confirm Password</label>
<input type="password" name="cpass" value="" class="a-text" />
</div>
<div>
<label>First Name</label>
<input type="text" name="fname" value="" class="a-text" />
</div>
<div>
<label>Last Name</label>
<input type="text" name="lname" value="" class="a-text" />
</div>
<div>
<label>Email</label>
<input type="email" name="email" value="" class="a-text" />
</div>
<div>
<label>Mobile</label>
<input type="text" name="mobile" value="" class="a-text" maxlength="10" />
</div>
<input type="submit" name="register" value="Register" class="button" id="button-left" />
</form>
</div>
What should I do add username and email validation? Please help me out friends.