Form Validation tutorial with CodeIgniter in PHP

 <!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <style>
        .error {
            color: red
        }
    </style>
    <title>Registration Form</title>
</head>

<body>
    <div class="jumbotron text-center">
    </div>
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-10">
            <form method="post" action="<?php echo base_url("/Registration/add"); ?>">

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="name">Name</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" placeholder="Enter name" name="userName" id="userName" value="<?php echo set_value("userName"); ?>">
                    </div>
                    <?php echo form_error("userName"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="Email">Email</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" placeholder="Enter Email" name="emailId" id="emailId" value="<?php echo set_value("emailId"); ?>">
                    </div>
                    <?php echo form_error("emailId"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="name">Phone No</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" placeholder="Enter Phone No" name="phoneNo" id="phoneNo" value="<?php echo set_value("phoneNo"); ?>">
                    </div>
                    <?php echo form_error("phoneNo"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="name">Password</label>
                    <div class="col-sm-4">
                        <input type="password" class="form-control" placeholder="Enter Password" name="password" id="password" value="<?php echo set_value("password"); ?>">
                    </div>
                    <?php echo form_error("password"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="name">Confirm Password</label>
                    <div class="col-sm-4">
                        <input type="password" class="form-control" placeholder="Confirm Password" name="confirm_password" id="confirm_password" value="<?php echo set_value("confirm_password"); ?>">
                    </div>
                    <?php echo form_error("confirm_password"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-4"></div>
                    <div class="col-sm-4">
                        <button type="submit" class="btn btn-primary">Add</button>
                        <a href="#" class="btn btn-danger">Back</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>

</html>






<?php
defined("BASEPATH") or exit("No direct script access allowed");

class Registration extends CI_Controller
{
    //If we want to call any library in All Controller then we have to call that library in
    //autoload.php and if we want to call any library in all functions of any one particular Controller class
    //then we have to call that library in constructor of that particular Controller class.
    public function __construct()
    {
        parent::__construct();
        $this->load->library("form_validation");
    }

    public function index()
    {
        echo "call registration function";
    }

    public function add()
    {
        if ($this->input->post()) {
            $this->form_validation->set_error_delimiters("<div class='error'>", "</div>");
            $this->form_validation->set_rules("userName", "Username", "trim|required|callback_checkName");
            $this->form_validation->set_rules("emailId", "Email", "trim|required|valid_email");
            $this->form_validation->set_rules("phoneNo", "Phone Number", "required");
            $this->form_validation->set_rules("password", "Password", "required");
            $this->form_validation->set_rules("confirm_password", "Confirm Password", "required|matches[password]");

            $this->form_validation->set_message("required", "{field} must be filled");
            $this->form_validation->set_message("valid_email", "Email address must be a valid email");
            if ($this->form_validation->run() == FALSE) {
                echo "Error";
                echo validation_errors();
            } else {
                echo "Success";
            }
        }
        $this->load->view("reg_form");
    }

    public function checkName($str)
    {
        if ($str == "test") {
            $this->form_validation->set_message("checkName", "The {field} field can not be the word 'test'");
            return FALSE;
        } else {
            return TRUE;
        }
    }
}





Comments