Welcome Guest, Not a member yet? Register   Sign In
CI 2.2 Form Validation not working as expected
#5

(This post was last modified: 10-27-2015, 12:01 AM by joseph.dicdican. Edit Reason: Edition on input pattern for phone )

Hi,

Based on what I understood.
Quote:If no input is supplied for the Email and Phone input field no error should be issued, but one is.

You want to validate email and phone if they are filled and if not, there should be no validation. If that's the case, just a suggestion,
for me, I will set the validation for email and phone after submitting the form. By checking if those two inputs are not null, then I will set the validation respectively.

PHP Code:
public function methodName() 
{
 
   $this->load->library('form_validation');
 
   $this->form_validation->set_rules('firstname''First Name''required|xss_clean');
 
   
    
// set validation rules for email or phone if not null
 
   if($_POST['email'] != null || $_POST['phone'] != null) { 
 
       $this->form_validation->set_rules('email''Email''trim|required|valid_email|xss_clean');
 
       $this->form_validation->set_rules('phone''Phone''trim|required|some_regex_validation|xss_clean');
 
   }

 
   if($this->form_validation->run()) {
 
       //do your process here
 
   }


There are also other way to validate your email and phone. If you like a quick validation, you can use the HTML5 input types.
For email, you can use type email. For phone validation, you can also use pattern in validation. 
Please refer here : http://www.w3schools.com/tags/att_input_pattern.asp 
Code:
<input type="email" name="email" />
<input type="text" name="phone" pattern="^d{2,3}-d{4}$" title="Please follow the format. (e.g. 032-0000)" />


These are just options, it depends on you what is best fitted for the project you are working on.
Thank you. Hope I helped you somehow.
- joseph.dicdican  Big Grin
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern
Passerelles numériques Philippines | Scholar
[email protected] 
Reply


Messages In This Thread
RE: CI 2.2 Form Validation not working as expected - by joseph.dicdican - 10-26-2015, 11:50 PM



Theme © iAndrew 2016 - Forum software by © MyBB