How to implement JS validations through CodeIgniter |
[eluser]Sumon[/eluser]
Hello all, I am very new in CodeIgnIter world. But within this time i love CI very much. I am used to validate a form like this.... JAVASCRIPT FUNCTION -------------------------------------------------------------------- function Validation() { if(this.frmRegistration.txtFullName.value=="") { alert("Please enter full name."); this.frmRegistration.txtFullName.focus(); return false; } return true; } -------------------------------------------------------------------- How shall i implement the same in CI. Please help me to get out..... thanks, Sumon
[eluser]xwero[/eluser]
You have to use the validation library to do this Code: $rules['txtFullName'] = 'required';
[eluser]nmweb[/eluser]
Are you looking for something like this: http://h1368840.stratoserver.net/2008/02...alidation/ So have the library generate the js? Otherwise you still have to write the js yourself.
[eluser]adamp1[/eluser]
I would highly disapprove of JS validation. First off if the user doesn't have JS turned on your form won't be validated. Also they could change the JS to allow invalid input. It would be fine if you used it in conjunction with the CI validation, but don't use it on its own.
[eluser]Sumon[/eluser]
Thanks for your help. I like to know something more. Here is my files ------------------------------------------------------- view_validate.php // inside view folder ------------------------------------------------------- echo form_open("JsValidation/ValidateMethod"); $data = array( 'name' => 'txtFullName', 'id' => 'txtFullName', 'value' => '', 'maxlength' => '100', 'size' => '50', 'style' => 'width:50%', ); echo form_input($data); echo form_submit('mysubmit', 'Submit Post!'); $rules['txtFullName'] = 'required'; $this->validation->set_rules($rules); $fields['txtFullName'] = 'full name'; $this->validation->set_fields($fields); ------------------------------------------------------- And here is my controller code ------------------------------------------------------- class JsValidation extends Controller { function JsValidation() { parent::Controller(); $this->load->helper("form"); $this->load->library('validation'); } function Validate() { $this->load->view("view_validate"); } } ------------------------------------------------------- Now my question is how to set and display error message. Anything to change in configuration file? Please help me to get out.
[eluser]xwero[/eluser]
You should add the rules and fields to the controller. To display the error all you have to do is Code: <?php echo $this->validation->error_string; ?>
[eluser]Sumon[/eluser]
I shall be highly happy if someone send me a complete example please. thanks, Sumon
[eluser]xwero[/eluser]
Code: /*------------------------------------------------------- |
Welcome Guest, Not a member yet? Register Sign In |