Web Analytics Made Easy -
StatCounter JavaScript Question : assigning Classes / Validation based on Class - CodingForum

Announcement

Collapse
No announcement yet.

JavaScript Question : assigning Classes / Validation based on Class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • JavaScript Question : assigning Classes / Validation based on Class

    I have a bit of a conundrum here.

    I am attempting to streamline my forms since my employer told me I could no longer work on my Form Generator project. Why? They're idiots.

    Anyway,

    Basically I have a standard form with a lot of inputs.

    At the top of the page I have this piece of PHP Code:

    Code:
    <?php 
    
    // dB Connects
    include("pm_inc.php");
    
    	$forForm = $_GET['forForm'];
    
    		// PHP will grab all dropdown values from dD_fields dB table
    		
    		$currentList = null;
    		$aSQL = "SELECT * FROM dD_fields WHERE forForm='$forForm'";
    		$aQRY = mysql_query($aSQL) or die(mysql_error());
    	$dD_values.="<span id=\"inv_dDValues\">";
    		while($aRES = mysql_fetch_array($aQRY))
    		{
    			// PHP will Assemble a series of <UL> Elements in the inv_dDValues SPAN
    			if($currentList != $aRES['inputId'] && $currentList != null)
    			{
    				$dD_values.="</ul>";
    			}
    			if($currentList != $aRES['inputId'])
    			{
    				$dD_values.="<ul id=\"".$aRES['inputId']."\">";
    				$currentList = $aRES['inputId'];
    			}
    			$dD_values.="<li>".$aRES['optionValue']."</li>";
    			$dD_values.="<li>".$aRES['optionDisplay']."</li>";
    		}
    		
    		$dD_values.="</span>"; 
    		echo $dD_values;
    		
    		// PHP will grab all validation information from inputVal dB table
    		$bSQL = "SELECT * FROM inputVal WHERE forForm='$forForm'";
    		$bQRY = mysql_query($bSQL) or die(mysql_error());
    	$valInfo.="<span id=\"inv_valInfo\">";
    		while($bRES = mysql_fetch_array($bQRY))
    		{
    			// PHP will Assemble a series of <UL> Elements in the inv_valInfo SPAN
    			$valInfo.="<ul id=\"".$bRES['inputId']."\">";
    			$valInfo.="<li>".$bRES['valRoutine']."</li>";
    			$valInfo.="</ul>";
    		}
    		
    		$valInfo.="</span>";
    		echo $valInfo;
    
    ?>
    This assembles a whole bunch of hidden-to-the-user unordered lists containing all my information on Validation Routines for each element in the form and DropDown Options. I use JavaScript to assemble my dropdown lists, each list id corresponds with a form element's ID/Name attribute and I simply append the options to the dropdown element.

    If anybody is interested more in how I did this I think it's pretty neat so I wouldn't mind sharing.

    The problem I'm running into is with my validation info. As you can see from the code I have put this information into lists as well. I then go through the lists and assign the $bRES['valRoutine'] variable to the class attribute of each form element.

    My valiDate() function, looks at each element on the page and identifies each element's class attribute. Then, depending on the class of the element, will run a different validation routine (optional, required (text), required (number), required (email) etc . . . ). I run valiDate() onSubmit, and assign my valRoutine's to my form elements onLoad.

    When I hard-code the valRoutine info into the class of each element, my validation works fine. And when I alert the class of an element I have just assigned a class to, it appears that the elements are actually being assigned proper classes. However, when I go to submit the form, validation does not work.

    My question is, should it work? Is it even possible to assign a class with the DOM and then valiDate with that assigned class?

  • #2
    Nevermind. For IE you must element.setAttribute("className",value) rather than element.setAttribute("class",value).

    I'm an idiot.

    Comment

    Working...
    X