Form Validation with CodeIgniter in PHP
<?php
defined("BASEPATH") or exit("No direct script access allowed");
//Form Validation
//=================
// -set_rules()
// -run()
//=================
//$this->form_validation->set_rules("name_of_input_field","Label to display error msg","rules");
//
//required|min_length[3]|max_length[25]
//echo form_error("name_of_input_field");
class Contact extends CI_Controller
{
public function index()
{
$this->load->library("form_validation");
$this->form_validation->set_rules("name", "Name", "required|min_length[3]", array(
"required" => "Name is required field",
"min_length[3]" => "The length of name must be at least 3",
));
$this->form_validation->set_rules("email", "Email", "required|valid_email", array(
"required" => "Email is required Field",
"valid_email" => "Email must be in valid format",
));
$this->form_validation->set_rules("mobile", "Mobile", "required|numeric|exact_length[10]", array(
"required" => "Mobile number is required field",
"numeric" => "Mobile number must be numeric and should contains numbers only",
"exact_length[10]" => "The length of mobile number must be exact 10 digits",
));
if ($this->form_validation->run() == TRUE) {
echo "All Okey";
} else {
$this->load->view("contact_view");
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
</head>
<body>
<h1>Contact Form</h1>
<?php echo validation_errors() . "<br>"; ?>
<?php echo form_open(); ?>
<table>
<tr>
<td>Name</td>
<td>
<input type="text" name="name" id="name" value="<?php echo set_value("name"); ?>">
<span><?php echo form_error("name"); ?></span>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text" name="email" id="email" value="<?php echo set_value("email"); ?>">
<span><?php echo form_error("email"); ?></span>
</td>
</tr>
<td>Mobile</td>
<td>
<input type="text" name="mobile" id="mobile" value="<?php echo set_value("mobile"); ?>">
<span><?php echo form_error("mobile"); ?></span>
</td>
<tr>
<td></td>
<td>
<input type="submit" name="save" value="Send">
</td>
</tr>
</table>
<?php echo form_close(); ?>
</body>
</html>


Comments
Post a Comment