How to validate mobile phone in codeigniter?

Member

by lottie , in category: PHP CMS , 6 months ago

How to validate mobile phone in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 6 months 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.