How to validate an ajax form with javascript?

Discussion in 'JavaScript' started by 123GoToAndPlay, Aug 16, 2007.

  1. #1
    hi,

    My particular form start with 1 main pulldown, depending on the choice either formfieldsA.php or forrmfieldsB.php will be called by an ajax request.

    But now i don't know how to set up the JS validation, like i have something in the line of:
    
    <form name="Form" action="?action=step3" method="POST" onsubmit="return Validator(this);">
    <select name="segment" id="segment"  onchange="javascript:segmentForm2();">
    	<option value="">Segment</option>
          <option value="formfieldsA.php">A</option>
    	  <option value="formfieldsB.php">B</option>
        </select>
    <div id="formfields" style="display:none;">
    
    </form>
    
    Code (markup):
    Based on segment the fields from formfieldsA.php or forrmfieldsB.php will be called by an ajax request.

    now for the validation part in index.php
    
    
    function Validator(theForm){
    if ( document.Form.segment.value==0)
       { alert('segment'); document.Form.segment.focus(); return false;}
       
    if ( document.Form.cName.value.length<1)
       { alert('name'); document.Form.cName.focus(); return false;}
    
    .....
    }
    
    Code (markup):
    works for all the fields in formfieldsA.php. But how do i check the fields in formfieldsB.php

    Any ideas??
     
    123GoToAndPlay, Aug 16, 2007 IP
  2. bibel

    bibel Active Member

    Messages:
    289
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You have to check first what formfield is selected
    Now you only check if no formfield is selected :

    if ( document.Form.segment.value==0)

    You need to add :

    if ( document.Form.segment.value=='formfieldsA.php'), and formfieldsB.php

    and validate the fields according to the selected form
     
    bibel, Aug 17, 2007 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    @bibel, seems to work like a charm.

    You help me out a lot, today. Tx!
     
    123GoToAndPlay, Aug 17, 2007 IP