• Server side form validation using regular expression

    Client side validation using Javascript is good but what about when a person disables javascript in his/her browser?  So in this post I have given a simple example about “Server Side Form Validation using Regular Expressions “.


    validation.php

    /* Contains eregi — Case insensitive regular expression match. */

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    
     
    <?php
    if($_POST)
    {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $gender = $_POST['gender'];
    // Full Name
    if (eregi('^[A-Za-z0-9 ]{3,20}$',$name))
    {
       $valid_name=$name;
    }
    else
    {
       $error_name='Enter valid Name.';
    }
     
    // Email
    if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email))
    {
       $valid_email=$email;
    }
    else
    {
       $error_email='Enter valid Email.';
    }
     
    // Usename min 2 char max 20 char
    if (eregi('^[A-Za-z0-9_]{3,20}$',$username))
    {
       $valid_username=$username;
    }
    else
    {
       $error_username='Enter valid Username min 3 Chars.';
    }
     
    // Password min 6 char max 20 char
    if (eregi('^[A-Za-z0-9!@#$%^&*()_]{6,20}$',$password))
    {
       $valid_password=$password;
    }
    else
    {
      $error_password='Enter valid Password min 6 Chars.';
    }
     
    // Gender
    if ($gender==0)
    {
       $error_gender='Select Gender';
    }
    else
    {
       $valid_gender=$gender;
    }
     
    if((strlen($valid_name)>0)&&(strlen($valid_email)>0)
    &&(strlen($valid_username)>0)&&(strlen($valid_password)>0) && $valid_gender>0 )
    {
       mysql_query(" SQL insert statement ");
       header("Location: thanks.html");
    }
    else
    { }
     
    }
    ?>

    index.php

    /* Contains HTML code. You have to include validation.php file. */

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <php include("validation.php"); ?>
    <form method="post" action="" name="form">
    Full name : <input type="text" name="name" value="<?php echo $valid_name; ?>" />
    <?php echo $error_name; ?>
    Email : <input type="text" name="name" value="<?php echo $valid_email; ?>" />
    <?php echo $error_email; ?>
    Username : <input type="text" name="name" value="<?php echo $valid_username; ?>" />
    <?php echo $error_username; ?>
    Password : <input type="password" name="name" value="<?php echo $valid_password; ?>" />
    <?php echo $error_password; ?>
    Gender : <select name="gender">
    <option value="0">Gender</option>
    <option value="1">Male</option>
    <option value="2">Female</option>
    </select>
    <?php echo $error_gender; ?>
    </form>

Comments on this post

Leave a Reply

  • Security Code :


    5 − = three