lundi 20 avril 2015

How to validate the username textfield in a form in php

I want to check in the username text field to be typed only letters how can i do it in PHP? Plus in the password area to input both letters and numbers. When I click the Register button it displays Successful Registration although the form is not filled and I also put an else statement to display a message to fill all the fields.

the form

<form action = "register.php" method = "POST">


    Username: <input type="text" name="username" > <br /><br/>
    Password: <input type="password" name="password"> <br /><br/>
    Confirm Password: <input type="password" name="repassword"> <br /><br/>

    Type:
    <select name="type">
    <option value="Choose">Please select..</option>
    <?php
    $sql=mysql_query("SELECT type FROM type");

    while($row=mysql_fetch_array($sql)){

        echo "<option value='".$row['type']."'>".$row['type']."</option>";
    }
    ?>
    </select><br/><br/>

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

the register code

     <?php

 require('connect.php');

$username=$_POST['username'];
$password=$_POST['password'];
$repass=$_POST['repassword'];
$type=$_POST['type'];

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

    //input only letters in username textfield

    if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['type'])){

        $sql = "INSERT INTO users (username, password, type) VALUES ('$username', '$password', '$type')";

        $result = mysql_query($sql);

        echo "Successful Registration";
    }

    if(!$result){

        $msg = "User Failed to be Registered.";  
    }
}

else{
     echo "Please fill all the fields.";
}

?>

Aucun commentaire:

Enregistrer un commentaire