Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
7.4 kB
3
Indexable
Never
<?php

$mobile_num_err = $gender_err = $department_err = $email_add_err = $first_name_err = $second_name_err = "";

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

  if($_REQUEST['first_name']){
    $first_name = $_REQUEST['first_name'];
    if(!ctype_upper($first_name[0]) && $first_name[0]!=' '){
      $first_name_err = 'First Letter must be captial';
    }else if($first_name[0]==' '){
      $first_name_err = 'Can not start with whitespace.';
    }else if (preg_match('~[0-9]+~', $first_name)) {
      $first_name_err = 'Name cannot contain any number.';
    }else if(!(preg_match("/^[a-zA-Z\s]+$/", $first_name))){
      $first_name_err = 'Name canot contain any special character.';
    }
  }else{
    $first_name_err = 'First name is required';
  }

  if($_REQUEST['second_name']){
    $second_name = $_REQUEST['second_name'];
    if(!ctype_upper($second_name[0]) && $second_name[0]!=' '){
      $second_name_err = 'First Letter must be captial';
    }else if($second_name[0]==' '){
      $second_name_err = 'Can not start with whitespace.';
    }else if (preg_match('~[0-9]+~', $second_name)) {
      $second_name_err = 'Name cannot contain any number.';
    }else if(!(preg_match("/^[a-zA-Z\s]+$/", $second_name))){
      $second_name_err = 'Name canot contain any special character.';
    }
  }else{
    $second_name_err = 'Second name is required';
  }

  if($_REQUEST['email']){
    $email = $_REQUEST['email'];
    $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^";  
    if (!preg_match ($pattern, $email) ){  
        $email_add_err = "Email is not valid.";  
    }
  }else{
    $email_add_err = 'Email is required';
  }

  if($_REQUEST['mobile']){
    $mobile = $_REQUEST['mobile'];
    if (!preg_match ("/^[0-9]*$/", $mobile) ){  
      $mobile_num_err = "Only numeric value is allowed.";  
    }else if(strlen($mobile)!=11){
      $mobile_num_err = "Mobile number must be 11 digits.";
    }  
  }else{
    $mobile_num_err = 'Mpbile Number is required';
  }

  if($_REQUEST['department']){
    $department = $_REQUEST['department'];
    if($department ==""){
      $department_err = 'Department Can not empty.';
    }
    //here check from DB
  }else{
    $department_err = 'Department is required';
  }

  if($_REQUEST['gender']){
    $gender = $_REQUEST['gender'];
    if($gender=="male" || $gender=="female"){
      
    }else{
      $gender_err = "Invalid Gender Selection";
    }
  }else{
    $gender_err = 'Must select one.';
  }
  

  
}


?>


<!DOCTYPE html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />

        <title>Hello, world!</title>
    </head>
    <body>
        <div class="container">
            <h1>Validation Form</h1>
            <form>
                <div class="row">
                    <div class="col">
                        <label>Enter First Name</label>
                        <input type="text" class="form-control" name="first_name" placeholder="Enter First Name" />
                        <?php if(!$first_name_err==""){echo "<span style='margin-top:8px;' class='badge badge-pill badge-danger'>".$first_name_err."</span>";}?>
                    </div>
                    <div class="col">
                        <label>Enter Second Name</label>
                        <input type="text" class="form-control" name="second_name" placeholder="Enter Second Name" />
                        <?php if(!$second_name_err==""){echo "<span style='margin-top:8px;' class='badge badge-pill badge-danger'>".$second_name_err."</span>";}?>
                    </div>
                </div>

                <div class="row">
                    <div class="col">
                        <label>Enter Your Email</label>
                        <input type="email" class="form-control" name="email" placeholder="Enter Email Address" />
                        <?php if(!$email_add_err==""){echo "<span style='margin-top:8px;' class='badge badge-pill badge-danger'>".$email_add_err."</span>";}?>
                    </div>
                    <div class="col">
                        <label>Enter Your Mobile Number</label>
                        <input type="number" class="form-control" name="mobile" placeholder="Enter Mobile Number" />
                        <?php if(!$mobile_num_err==""){echo "<span style='margin-top:8px;' class='badge badge-pill badge-danger'>".$mobile_num_err."</span>";}?>
                    </div>

                    
                    
                </div>

                <div class="row">

                <div class="col">
                        <label>Department</label>
                        <select class="form-control" name="department">
                          <option value="">Slect Department</option>
                          <option value="CSE">CSE</option>
                          <option value="EEE">EEE</option>
                        </select>
                        <?php if(!$email_add_err==""){echo "<span style='margin-top:8px;' class='badge badge-pill badge-danger'>".$department_err."</span>";}?>
                    </div>

                  <div class="col">
                  <label>Select One</label>
                  <div class="form-check">
                    <input class="form-check-input" type="radio" name="gender"  id="exampleRadios1" value="male" checked>
                    <label class="form-check-label" for="exampleRadios1">
                      Male
                    </label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="radio" name="gender" id="exampleRadios2" value="female">
                    <label class="form-check-label" for="exampleRadios2">
                      Female
                    </label>
                  </div>
                  <?php if(!$gender_err==""){echo "<span style='margin-top:8px;' class='badge badge-pill badge-danger'>".$gender_err."</span>";}?>
                  </div>
                </div>

                <button style="margin-top: 15px;" type="submit" class="btn btn-primary" name="submit">Submit</button>
            </form>
        </div>

        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    </body>
</html>