SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Location
    Canada,Montréal
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    alert at end of function wont show up becaus of 2 lines help plz

    Hi

    Im bulding a form and during the validation i found out that two line of code are causing problem.

    If i remove them everything is fine.Unfortunately i need them to hide the spans with the .invalide class.

    I want the spans to be visible only if there is an error.

    Here is my webpage : http://kmultim.com/index.php?idSection=contact

    'Clicking on Envoyer start the fonction'

    If i remove thes two line the alert will pop-up
    .
    Code:
    		
    erreurSpan[i].innerHTML ='';
    erreurSpan[i].style.display ='none';
    Code:
    function validForm(oForm)
    {
    	var erreur = 0; 
    	var erreurSpan = document.getElementsByClassName('invalide')// liste des span avec la classe error
    	//var regExpCourriel = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i	;
    	for(var i = 0; i < oForm.elements.length ; i++)
    	{		
    		
    		erreurSpan[i].innerHTML ='';// buggy line 1
    		erreurSpan[i].style.display ='none'; // buggy line 2
    		var laValeurInp = oForm.elements[i].value
    
    		if(oForm.elements[i].className == "inputForm") // get the element of the form with the class inputForm
    		{	
    			
    			if(laValeurInp == "")  // input or text is empte
    			{
    				erreurSpan[i].innerHTML ="Ce champ ne doit pas &ecirc;tre vide";
    				erreurSpan[i].style.display ="block";
    				erreur = 1;				
    			}else
    				{
    					
    					if(laValeurInp.length < 2) //input or text is less than 2 characters
    					{
    						erreurSpan[i].innerHTML ="Ce doit contenir au moin deux charact&egrave;res";
    						erreurSpan[i].style.display ="block";
    						erreur = 1;	
    					}
    				}
    
    		}
    		
    	}
    	alert(1);
    }

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The reason the 2 lines are causing an error is because your counting the length of all the elements in the form instead of just the span elements, if you change the argument to...

    Code:
    for (var i = 0; i < erreurSpan.length; i++)
    then your code should work fine after that.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Location
    Canada,Montréal
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes but if the condition if(oForm.elements[i].className == "inputForm") would filter those unneeded element . your sugestion remove the need for it .

    I just remembered that found getElementsByClassName () doesn't exist in internet explorer.
    var erreurSpan = document.getElementsByClassName('invalide'); // this dosent work

    i used var erreurSpan = $('.invalide'); with jquery since im already using jquery for something else in the page.

    Now everything is worcking fine thanks .


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •