0

i want to validate a form in clint side in codeignigter. i have a view and a javascript function. how can i call the javascript function to validate the form. here is my code

    function validate()
    {
var txtSchoolName=document.getElementById('name');
var address=document.getElementById('address');
var contactNo1=document.getElementById('contactNo1');

    if((name.value==null)||(name.value=="")){
    alert("Enter School Name")
    name.focus()
    return false
    }
if((address.value==null)||(address.value=="")){
    alert("Provide Address")
    address.focus()
    return false
    }
if((contactNo1.value==null)||(contactNo1.value=="")){
    alert("Provide Contact Number")
    contactNo1.focus()
    return false
    }
   return true
  }
  </script>
  echo form_open_multipart('register_school/updateSchool');
    echo '<p>';

    echo '</br>';
    echo form_label('School Name : ','username',$attributes);
    echo form_input('name');

    echo form_label('School Address : ','username',$attributes);
    echo form_input('address');

    echo form_label('Contact No 1 : ','username',$attributes);
    echo form_input('contactNo');
            echo form_submit('submit', 'Save','validate()');        
    echo form_close();
3
  • Might sound a silly question but, do you want to validate it on submit? Commented Apr 9, 2013 at 13:57
  • I see you are already using the validate() function on form_submit, but, don't have to use it like 'onSubmit="validate()"' ? Commented Apr 9, 2013 at 14:01
  • yes i want a validate on submit Commented Apr 9, 2013 at 14:03

1 Answer 1

2

I belive the right usage for form_submit third parameter is the same as for form_checkbox:

from the CI docs:

$js = 'onClick="some_function()"';

echo form_checkbox('newsletter', 'accept', TRUE, $js)

so your code should be

echo form_submit('submit', 'Save', 'onClick="validate()"');  

or

 echo form_open_multipart('register_school/updateSchool', 'onSubmit="validate()"');
Sign up to request clarification or add additional context in comments.

2 Comments

I edited my answer. Try it again please. The onSubmit event is only for the form. For the submit button it's the onClick event
i don't have 15 reputation so i could not mark it as answer. sorry for that. but thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.