4 Replies - 303 Views - Last Post: 20 January 2012 - 10:56 AM

Topic Sponsor:

#1 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 415
  • Joined: 14-April 11

calling several javascript functions at the form submit

Posted 20 January 2012 - 09:19 AM

Hi all,

I have a programme where I have 50 questions . I have written js functions to validate each.

but now problem is at form I cant't call every methode.
so I have to put all code into one methode and check.

Is it ok? are there any alternative option?

thx in advanc....
Is This A Good Question/Topic? 0
  • +

Replies To: calling several javascript functions at the form submit

#2 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 415
  • Joined: 14-April 11

Re: calling several javascript functions at the form submit

Posted 20 January 2012 - 10:34 AM

hi here is the code
 <script type="text/javascript">
        function validat_UserInputs()
        {
          return checkRadioQ1(); 
          return  checkRadioQ2();
          return true;
        }
        
        function checkRadioQ1()
        {
            
              var radios = document['form1'].elements['gender']; 
                 for (var i=0; i <radios.length; i++) { 
                  if (radios[i].checked) { 
                  
                  }else
                      {
                          alert("Question 1 is not completed ! ");
                           return false;
                      }
                      
                 }
                 
                     
                            
        }
      
        function checkRadioQ2()
        {
            
            var radios2 = document['form1'].elements['ace']; 
                 for (var i=0; i <radios2.length; i++) { 
                  if (radios2[i].checked) { 
                   
                  }
                  else
                      {
                          alert("Question 2 is not completed ! ");
                           return false;
                      }
                      
                 }
        }



Problem is alway alert the "Question 1 is not completed ! " thought I checked a radio button from group 1. how to correct this?

thx inadvance...
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,429
  • Joined: 08-June 10

Re: calling several javascript functions at the form submit

Posted 20 January 2012 - 10:45 AM

checkRadioQ1() checks if every radio button with the name gender is checked, which is impossible (you can only check one radio button of a group with the same name).

if your browser supports CSS3’s pseudo-class :checked and all the buttons have an exclusive class name you coud use
if (0 === document.querySelectorAll(".gender:checked").length) {
    // radio button group is unchecked
}


even some scope magic is possible
var gender = document.form1.gender;
var checked = [].filter.call(gender, function(item) { 
    return item.checked; 
});
if (0 === checked.length) {
    // radio button group is unchecked
}

This post has been edited by Dormilich: 20 January 2012 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 415
  • Joined: 14-April 11

Re: calling several javascript functions at the form submit

Posted 20 January 2012 - 10:48 AM



<?php    $attri=array('name'=>"form1",'onsubmit'=>"return validat_UserInputs()");
   echo form_open('welcome/saveData',$attri); ?> 
    <ol type="1" style="margin: 35px;">
        <li>  
                <label>Gender</label><br>
                <label>Male </label> <input type="radio" name="gender" value="M" />
                <label>Female </label><input type="radio" name="gender" value="F" /> 
           

        </li>
        <br>
        <br>
        <li><label>Race</label><br> 
            Sinhalese <input type="radio" name="ace" value="sinhalese" />
            Tamil <input type="radio" name="ace" value="tamil" />   
            Muslim <input type="radio" name="ace" value="muslim" />
            Burgher <input type="radio" name="ace" value="burger" /> 
            Malay <input type="radio" name="ace" value="malay" />
            Other <input type="radio" name="ace" value="other" />
        </li><br><br>

</form>



thx,
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,429
  • Joined: 08-June 10

Re: calling several javascript functions at the form submit

Posted 20 January 2012 - 10:56 AM

see previous post.

additionally, <br> is no allowed inside <ul> and <ol>. use padding or margin to fix the distance.

This post has been edited by Dormilich: 20 January 2012 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1