How to validate form in codeigniter?
Server Side Form validation in codeigniter
Form Validation Rule
File Name :
$this->load->library('form_validation');
//$this->form_validation->set_rules('ref_no', 'Ref No','trim|required|callback_my_custom_rule');
$this->form_validation->set_rules('ref_no', 'Ref No','trim|required|callback_alpha_numeric_dash_space');
$this->form_validation->set_rules('ittutorial_location', 'ittutorial Location','trim|required');
//$this->form_validation->set_rules('applicant_name', 'Applicant Name','trim|required|callback_alpha_space');
$this->form_validation->set_rules('applicant_name', 'Applicant Name','trim|required|regex_match[/^[][a-zA-Z ()]+$/]',array('required'=>'Please Enter Applicant Name!','alpha'=>'Only alphabets please!'));
// array('field'=>'ptitle','label'=>'Property Title','rules'=>'trim|required|xss_clean|regex_match[/^[][a-zA-Z0-9@# ,().]+$/]');
//$this->form_validation->set_rules('applicant_name', 'Applicant Name','trim|required|alpha');
$this->form_validation->set_rules('designation', 'Designation', 'trim|required|callback_alpha_space');
//$this->form_validation->set_rules('address', 'Address', 'trim|required');
$this->form_validation->set_rules('address', 'Address', 'trim|required', array('required'=>'Please Enter Address'));
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|regex_match[/^[0-9]{10}$/]'); // {10} for 10 digits number
//$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|numeric|exact_length[10]');
$this->form_validation->set_rules('mode_of_reciept', 'Mode of Reciept', 'trim|required');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required');
$this->form_validation->set_rules('received_date', 'Received Date', 'trim|required');
$this->form_validation->set_rules('info_required', 'Info Required', 'trim|required');
$this->form_validation->set_rules('pertaining_to_the_department', 'Pertaining to the Department', 'trim|required');
$this->form_validation->set_rules('date_of_transfer', 'Date of Transfer', 'trim|required');
/* ################### Custom Validation ##################### */
public function space_check($str)
{
$pos = strrpos($str, " ");
if ($pos === false) { // note: three equal signs
// not found...
$this->form_validation->set_message('space_check', 'The %s field must contain First name and Last Name');
return FALSE;
}
else {
return true;
}
}
public function customAlpha($str)
{
if ( !preg_match('/^[a-z .,\-]+$/i',$str) )
{
return false;
}
}
/*public function alpha_dash_space($str)
{
return ( ! preg_match("/^([-a-z_ ])+$/i", $str)) ? FALSE : TRUE;
}*/
public function alpha_space($str)
{
if (! preg_match('/^[a-zA-Z\s]+$/', $str)) {
$this->form_validation->set_message('alpha_space', 'The %s field contain only characters');
return FALSE;
} else {
return TRUE;
}
}
public function alpha_numeric_dash_space($str_in = '')
{
if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in))
{
$this->form_validation->set_message('alpha_numeric_dash_space', 'The %s field only contain alpha-numeric,underscores & dashes.');
return FALSE;
}
else
{
return TRUE;
}
}
/* public function checkDateFormat($date)
{
if(preg_match("/[0-31]{2}/[0-12]{2}/[0-9]{4}/", $date))
{
if(checkdate(substr($date, 3, 2), substr($date, 0, 2), substr($date, 6, 4)))
return true;
else
return false;
} else {
return false;
}
} */
/* ################### End Custom Validation ################## */
Form
File Name :
<section class="content">
<div class="container-fluid">
<div class="page-content-inner">
<div class="profile">
<h3 class="formheading text-center" style="font-size: 1.25rem;margin-bottom: 34px;">RTI Application Form</h3>
<?php //if(isset($_SESSION['temp']['success_msg'])&&($_SESSION['temp']['success_msg'])):?>
<!-- <center><span style="color:green;margin: 5%;font-size: 16px;font-weight: bold;"><?php //echo $_SESSION['temp']['success_msg']; ?></span></center> -->
<?php //endif;?>
<div class="formblock">
<?php //echo form_open_multipart("rti-submit" ,array('onsubmit' => 'return checkform()')); ?>
<form action="<?php echo base_url()?>rti-submit" method="post" name="form1" id="form1" enctype='multipart/form-data'>
<!--Card Start-->
<div class="card card-default">
<div class="card-header form-card-header">
<h3 class="card-title">RTI Application Form</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputKey" class="col-md-3 control-label">Ref. No. RTI<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="ref_no" id="ref_no" value="<?php echo set_value('ref_no'); ?>" placeholder=" Enter Ref. No. RTI" />
<span class="required" id="address_error" aria-required="true"></span>
<span class="error"><?php echo form_error('ref_no'); ?></span>
</div>
<label for="inputKey" class="col-md-3 control-label">RTI Received ittutorial Location<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<select class="form-control requireds selectpicker" name="ittutorial_location" id="ittutorial_location">
<option value="" selected="selected" disabled="disabled">-Select RTI Received ittutorial Location-</option>
<?php if(!empty($branch_info)){
foreach($branch_info as $row){
?>
<option value="<?php echo $row->pki_id; ?>"><?php echo $row->uvc_branch_code;?></option>
<?php } }?>
</select>
<span class="error"><?php echo form_error('ittutorial_location'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Applicant Name<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="applicant_name" id="applicant_name" value="<?php echo set_value('applicant_name'); ?>" placeholder=" Enter Applicant Full Name"/>
<span class="error"><?php echo form_error('applicant_name'); ?></span>
</div>
<label for="inputValue" class="col-md-3 control-label">Designation and Organization<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="designation" id="designation" value="<?php echo set_value('designation'); ?>" placeholder=" Enter Designation and Organization"/>
<span class="error"><?php echo form_error('designation'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Address<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<!-- onkeypress="return IsAlphaNumeric(event);" ondrop="return false;" onpaste="return false;" -->
<input type="text" class="form-control" name="address" id="address" value="<?php echo set_value('address'); ?>" placeholder=" Enter Address"
onkeyup="Validate_specialChar(this.id)" />
<span id="error" style="color: Red; display: none">* Special Characters not allowed.</span>
<span class="required" id="address_errr" aria-required="true"></span>
<span class="error"><?php echo form_error('address'); ?></span>
</div>
<label for="inputValue" class="col-md-3 control-label">Email<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="email" id="email" value="<?php echo set_value('email'); ?>" placeholder=" Enter Email" />
<span class="required" id="email_error" aria-required="true"></span>
<span class="error"><?php echo form_error('email'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Mobile/Phone No.<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="mobile" id="mobile" value="<?php echo set_value('mobile'); ?>" placeholder=" Enter Mobile/Tel-Phone" maxlength="10"/>
<span class="required" id="mobile_error" aria-required="true"></span>
<span class="error"><?php echo form_error('mobile'); ?></span>
</div>
<label for="inputKey" class="col-md-3 control-label">Mode of Receipt<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<select class="form-control requireds selectpicker" name="mode_of_reciept" id="mode_of_reciept">
<option value="" selected="selected" disabled="disabled">-Select Mode of Receipt-</option>
<?php foreach($modeof_receipt as $row)
{
?>
<option value="<?php echo $row->id; ?>"><?php echo $row->mode_of_receipt;?></option>
<?php } ?>
</select>
<span class="error"><?php echo form_error('mode_of_reciept'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Received Date<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="date" name="received_date" id="received_date" value="<?php echo set_value('received_date'); ?>" class="form-control"></input>
<span class="error"><?php echo form_error('received_date'); ?></span>
</div>
<label for="inputKey" class="col-md-3 control-label">Information Required<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="info_required" id="info_required" value="<?php echo set_value('info_required'); ?>" placeholder=" Enter Information Required" maxlrngth="500"/>
<span class="error"><?php echo form_error('info_required'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputKey" class="col-md-3 control-label">Subject<span class="required" aria-required="true"> * </span></label>
<div class="col-md-9">
<textarea class="form-control requireds" name="subject" id="subject" placeholder=" Enter Subject" rows="3" cols="50" onkeyup="ValidatespecialChar(this.id)" maxlength="500" ><?php echo set_value('subject'); ?></textarea>
<span class="error"><?php echo form_error('subject'); ?></span>
<span class="required" aria-required="true" style="color:green;float: right;">Maximum 500 characters are allowed</span></br>
<span class="required" id="subject_errr" aria-required="true"></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputKey" class="col-md-3 control-label">Pertaining to the Department<span class="required" aria-required="true"> * </span></label>
<div class="col-md-9">
<select class="form-control requireds selectpicker" name="pertaining_to_the_department" id="pertaining_to_the_department" onchange="getPertaining();">
<option value="" selected="selected" disabled="disabled">-Select Pertaining to the Department-</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<span class="error"><?php echo form_error('pertaining_to_the_department'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group" id="dept_div" style="display: none">
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputKey" class="col-md-3 control-label">Select Department:
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-9">
<select class="form-control" name="department[]" id="department" multiple>
<?php foreach($dept as $row){?>
<option value="<?php echo $row->pki_id;?>"><?php echo $row->uvc_department_name;?></option>
<?php }?>
</select>
<span class="input-group-addon"> </span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group" id="transfer_dept_div" style="display: none">
<div class="col-md-12">
<div class="form-group row">
<label for="inputKey" class="col-md-3 control-label">Transfer to Department:
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-9">
<select class="form-control" name="department[]" id="trans_department" multiple>
<?php foreach($dept as $row_dept){?>
<option value="<?php echo $row_dept->pki_id;?>"><?php echo $row_dept->uvc_department_name;?></option>
<?php }?>
</select>
<span class="input-group-addon"> </span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Date of Transfer<span class="required" aria-required="true"> * </span></label>
<div class="col-md-9">
<input type="date" name="date_of_transfer" id="date_of_transfer" value="<?php echo set_value('date_of_transfer'); ?>" class="form-control"></input>
<span class="error"><?php echo form_error('date_of_transfer'); ?></span>
</div>
</div>
</div>
</div>
<div class="morefiles">
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputKey" class="col-md-3 control-label">Upload Document</label>
<div class="col-md-7">
<input type="file" id="docfile1" name="attachment[]" class="form-control" onchange="Validatefile(this.id,this.value)" multiple>
<span class="required" id="upload_error" aria-required="true"></span>
<p style="color:green;">File format Allowed (.pdf, .jpg, .png) and Max. upload size of each file 5 MB</p>
</div>
<div class="col-md-2">
<button class="btn btn-success btn-sm" type="button" id="add" title='Add new file'/>Add More</button>
</div>
</div>
</div>
</div>
</div>
</div></div>
<!--Card End-->
<!--Footer-->
<div class="button_center">
<!-- <input type='submit' class="btn btn-primary waves-effect submit_doc" id="submit_complaint" onclick="submit_form()" value="INSERT RTI"/>-->
<button type='submit' class="btn btn-primary waves-effect" value="INSERT RTI">Submit</button>
</div>
<div id="msg_loader"><img src="<?php echo base_url(); ?>assets/dist/img/loader.gif"></div>
<?php echo "</form>";?>
</div>
</div>
</div>
</div>
</section>
Example
File Name :
$this->load->library('form_validation');
$this->form_validation->set_rules('audit_type', 'Audit Type', 'trim|required', array('required'=>'Please Enter Audit Type'));
$this->form_validation->set_rules('report_number', 'Report Number', 'trim|required|callback_alpha_numeric_dash_space',array('required'=>'Please Enter Report Number','alpha_numeric_dash_space'=>'Please Enter Only alpha numeric,Dash,Underscore'));
$this->form_validation->set_rules('para_number', 'Para Number', 'trim|required|callback_alpha_numeric_dash_space',array('required'=>'Please Enter Para Number','alpha_numeric_dash_space'=>'Please Enter Only alpha numeric,Dash,Underscore'));
$this->form_validation->set_rules('auditor_name', 'Auditor Name', 'trim|required|callback_alpha_space',array('alpha_space'=>'Please Enter Only Alpha Character'));
$this->form_validation->set_rules('address', 'Address', 'trim|required',array('required'=>'Please Enter Address!','alpha'=>'Only alphabets please!'));
$this->form_validation->set_rules('subject', 'subject', 'trim|required');
$this->form_validation->set_rules('audit_query', 'Audit Query', 'trim|required');
$this->form_validation->set_rules('audit_para_received_date', 'Received Date', 'trim|required');
//$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[ittutorialconnect_members_information.email]',
//array('required'=>'Please enter valid email address', 'valid_email'=>'Please provide enter email address','is_unique'=>'Duplicate Email Address'));
public function alpha_space($str)
{
if (! preg_match('/^[a-zA-Z\s]+$/', $str)) {
$this->form_validation->set_message('alpha_space', 'The %s field contain only characters');
return FALSE;
} else {
return TRUE;
}
}
public function alpha_numeric_dash_space($str_in = '')
{
if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in))
{
$this->form_validation->set_message('alpha_numeric_dash_space', 'The %s field only contain alpha-numeric, underscores & dashes.');
return FALSE;
}
else
{
return TRUE;
}
}
public function file_check($str){
$allowed_mime_type_arr = array('image/jpg','image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');
$mime = get_mime_by_extension($_FILES['photograph']['name']);
if(isset($_FILES['photograph']['name']) && $_FILES['photograph']['name']!=""){
if(in_array($mime, $allowed_mime_type_arr)){
return true;
}else{
$this->form_validation->set_message('file_check', 'Please select only jpg/gif/jpeg/png file.');
return false;
}
}else{
$this->form_validation->set_message('file_check', 'Please choose a file to upload.');
return false;
}
}
Form
File Name :
<section class="content">
<div class="container-fluid">
<div class="page-content-inner">
<div class="profile">
<h3 class="formheading text-center" style="font-size: 1.25rem;margin-bottom: 34px;">Audit Para Form</h3>
<?php //if(isset($_SESSION['temp']['success_msg'])&&($_SESSION['temp']['success_msg'])):?>
<!-- <center><span style="color:green;margin: 5%;font-size: 16px;font-weight: bold;"><?php //echo $_SESSION['temp']['success_msg']; ?></span></center> -->
<?php //endif;?>
<div class="formblock">
<?php echo form_open_multipart("save-audit-para"); ?>
<!--Card Start-->
<div class="card card-default">
<div class="card-header form-card-header">
<h3 class="card-title">Entry Form of Audit Para</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputKey" class="col-md-3 control-label">Audit Type<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<select class="form-control requireds selectpicker" name="audit_type" id="audit_type">
<option value="" selected="selected" disabled="disabled">-Select Audit Type-</option>
<?php if(!empty($audit_type)){foreach($audit_type as $row){ ?>
<option value="<?php echo $row->id;?>"><?php echo $row->audit_type_name;?></option>
<?php }}?>
</select>
<span class="error"><?php echo form_error('audit_type'); ?></span>
</div>
<label for="inputKey" class="col-md-3 control-label">Report No.<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="report_number" id="report_number" value="<?php echo set_value('report_number'); ?>" placeholder=" Enter Report Number" />
<span class="required" id="address_error" aria-required="true"></span>
<span class="error"><?php echo form_error('report_number'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Para No.<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="para_number" id="para_number" value="<?php echo set_value('para_number'); ?>" placeholder=" Enter Para Number"/>
<!-- onkeypress="return only_number_allow(this,event);" -->
<span id="error" style="color: Red; display: none">* Only Numbers allowed.</span>
<span class="required" id="para_number_errr" aria-required="true"></span>
<span class="error"><?php echo form_error('para_number'); ?></span>
</div>
<label for="inputValue" class="col-md-3 control-label">Auditor Name.<span class="required" aria-required="true"> * </span></label>
<div class="col-md-3">
<input type="text" class="form-control" name="auditor_name" id="auditor_name" value="<?php echo set_value('auditor_name'); ?>" placeholder=" Enter Auditor Number"/>
<span class="error"><?php echo form_error('auditor_name'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Address<span class="required" aria-required="true"> * </span></label>
<div class="col-md-9">
<textarea class="form-control requireds" name="address" id="address" placeholder=" Enter Address" rows="2" cols="50"><?php echo set_value('address'); ?><?php echo set_value('address'); ?></textarea>
<span class="required" id="address_error" aria-required="true"></span>
<span class="error"><?php echo form_error('address'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Subject.<span class="required" aria-required="true"> * </span></label>
<div class="col-md-9">
<textarea class="form-control requireds" name="subject" id="subject" placeholder=" Enter Subject" rows="2" cols="50" onkeyup="ValidatespecialChar(this.id)" maxlength="200"><?php echo set_value('subject'); ?><?php echo set_value('subject'); ?></textarea>
<span class="error"><?php echo form_error('subject'); ?></span>
<span class="required" aria-required="true" style="color:green;float: right;">Maximum 200 characters are allowed</span></br>
<span class="required" id="subject_errr" aria-required="true"></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Audit Para/Auditor Query<span class="required" aria-required="true"> * </span></label>
<div class="col-md-9">
<textarea class="form-control requireds" name="audit_query" id="audit_query" placeholder=" Enter Audit Para/Auditor Query" rows="4" cols="50" onkeyup="ValidatespecialChar_audit(this.id)" maxlength="500"><?php echo set_value('audit_query'); ?><?php echo set_value('audit_query'); ?></textarea>
<span class="error"><?php echo form_error('audit_query'); ?></span>
<span class="required" aria-required="true" style="color:green;float: right;">Maximum 500 characters are allowed</span></br>
<span class="required" id="audit_query_errr" aria-required="true"></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputValue" class="col-md-3 control-label">Audit Para Received Date<span class="required" aria-required="true"> * </span></label>
<div class="col-md-9">
<input type="date" name="audit_para_received_date" id="audit_para_received_date" value="<?php echo set_value('audit_para_received_date'); ?>" class="form-control"></input>
<span class="error"><?php echo form_error('audit_para_received_date'); ?></span>
</div>
</div>
</div>
</div>
<div class="morefiles">
<div class="form-group">
<div class="col-md-12">
<div class="form-group row">
<label for="inputKey" class="col-md-3 control-label">Document Attached</label>
<div class="col-md-7">
<input type="file" id="docfile1" name="attachment[]" class="form-control" onchange="Validatefile(this.id,this.value)" multiple>
<span class="required" id="upload_error" aria-required="true"></span>
<p style="color:green;">File format Allowed (.pdf, .jpg, .png) and Max. upload size of each file 5 MB</p>
</div>
<div class="col-md-2">
<button class="btn btn-success btn-sm" type="button" id="add" title='Add new file'/>Add More</button>
</div>
</div>
</div>
</div>
</div>
<input type="hidden" name="month" value=" <?php $today = date('Y-F-d');
$time=strtotime($today);
//echo $year=date("Y",$time);
echo $month=date("F",$time);
?>" />
<input type="hidden" name="year" value="<?php $today = date('Y-F-d');
$time=strtotime($today);
echo $year=date("Y",$time);
//echo $month=date("F",$time);
?>" />
</div></div>
<!--Card End-->
<!--Footer-->
<div class="button_center">
<input type='submit' class="btn btn-primary waves-effect submit_doc" id="submit_complaint" onclick="submit_form()" value="Submit"/>
</div>
<div id="msg_loader"><img src="<?php echo base_url(); ?>assets/dist/img/loader.gif"></div>
<?php echo "</form>";?>
</div>
</div>
</div>
</div>
</section>
Previous
Next