SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    statments in if and else execute?

    In this form (using this js file) validate() is called on submit.

    If the conditions in the if statment are met initWarning is called which highlights the fields red AND is supposed to create and insert a p element with a warning message. However that warning message will only appear when I comment out deleteWarningText() in the else block. So what did I mess up to make both if and else statements execute?

    Code JavaScript:
    function validate()
    {
    	var form = this;
    	var isValid = true;
    	var els = form.getElementsByTagName("input");
    	for (var i = 0; i < els.length; i++)
    	{
    		if (hasClass(els[i], "required") && (els[i].value == "" || /^\s+$/.test(els[i].value))) //if the field is empty
    		{
    			initWarning(els[i]); // display warning
    			isValid = false;
    		}
    		else
    		{
    			els[i].style.borderColor = "";
    			els[i].style.backgroundColor = "";
    			deleteWarningText();
    		}
    	}
    	return isValid;
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,718
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    There are multiple inputs being checked, so if one input fails, but the next one succeeds, that second one that succeeded removes the warning text.

    A solution to this is to remove the warning text before the loop occurs, so hat the loop then has the opportunity to put up warnings that get to stay there until the next validation occurs.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll probably use your solution, but if they all fail the warning text is still deleted. It only is not deleted if I comment out deleteWarningText().

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,718
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    This is because the for loop is going through all input fields, which include the submit and reset buttons, which cause the else part of the code to fire.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •