How to validate mobile phone in codeigniter?

Member

by lottie , in category: PHP CMS , a year ago

How to validate mobile phone in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , a year ago

@lottie 

To validate a mobile phone number in CodeIgniter, you can use the built-in form validation library provided by CodeIgniter. Here is an example of how you can validate a mobile phone number:

  1. Load the form validation library in your controller:
1
$this->load->library('form_validation');


  1. Set the validation rules for the mobile phone number field in your form validation configuration in the controller:
1
$this->form_validation->set_rules('mobile_phone', 'Mobile Phone', 'required|regex_match[/^+?d{10,15}$/]');


In this example, we are setting the validation rule for the 'mobile_phone' field to be required and to match the regular expression pattern for a valid mobile phone number format.

  1. Run the form validation in your controller:
1
2
3
4
5
6
7
if($this->form_validation->run() == FALSE) {
    // Validation failed
    $this->load->view('your_form_view');
} else {
    // Validation passed
    // Process the data
}


  1. Create a view file with the form input field for the mobile phone number:
1
2
3
4
5
6
<form method="post" action="your_controller/method">
    <label for="mobile_phone">Mobile Phone:</label>
    <input type="text" name="mobile_phone" id="mobile_phone">
    <?php echo form_error('mobile_phone'); ?>
    <button type="submit">Submit</button>
</form>


In this example, we have created a simple form with an input field for the mobile phone number. The form_error() function is used to display any validation errors associated with the 'mobile_phone' field.


By following these steps, you can validate a mobile phone number in CodeIgniter using the form validation library.

Related Threads:

How to validate phone number with zero at the front in php?
How to validate form in codeigniter?
How to validate address field in codeigniter?
How to validate value of array in codeigniter?
How to validate a single form field in codeigniter?
How to validate post request in symfony 5?