Articles → PHP CODEIGNITER → Form Validation in codeigniter

Form Validation in codeigniter






Steps

















View changes




<body>
	<div id="container">
		<div id="body"></div>
	</div>
	<?php echo form_open(); 
   echo form_input('username', "); 
   echo form_submit('mysubmit', 'Click Me'); 
   echo form close();?>
</body>
</html>



Controller Changes














public function Index() { 
$this->load->helper('form'); 
$this->load->library('form_validation'); 
$this->load->view('users'); 
$this->form validation->set rules('username', 'Username', 'required'); 
  if ($this->form_validation->run() == FALSE) { 
    echo "Please enter user name";
  } 
  else{ 
    echo "Valid user name";
  }
}





Output




Picture showing the view when page gets loaded

Click to Enlarge





Picture showing the validation message when page is submitted with empty textbox

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, August 5, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250