SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form validation bug -- require checkbox and input box

    I have two js validation scripts that I am trying to combine into one function. One validates the user input boxes and the other validates that AT LEAST ONE checkbox is selected.

    I have it working, however it is requiring that both checkboxes are checked, instead of any of them.

    HTML Code:
    <script>
    function checkFields(frm) {
    	missinginfo = "";
    	
    	if (document.loginform.name.value == "") {
    		missinginfo += "\n     -  Full Name";
    	}
    	
    	// get checkbox array
    	for (i=0; i<frm.length; i++)  {
    		if (frm.elements[i].id.indexOf('chkStatus') !=-1)    {
    		  	if (frm.elements[i].checked) {
    				// do nothing
    		  	} else {
    				missinginfo += "\n     -  Select a checkbox";
    			}
    		}
       	}
    
    	if (missinginfo != "") {
    		missinginfo ="_____________________________\n" +
    		"You failed to correctly fill in your:\n" +
    		missinginfo + "\n_____________________________" +
    		"\nPlease re-enter and submit again!";
    		alert(missinginfo);
    		return false;
    	}
    else return true;
    }
    
    </script>
    
    
    <form name="loginform" method="post" action="" onSubmit="return checkFields(loginform);">
    <input name="name" id="name"><br>
    <input name="markets[]" type="checkbox" id="chkStatus" value="Biomaterials" /><br>
    <input name="markets[]" type="checkbox" id="chkStatus" value="Biotechnology/Cell Therapy" /><br>
    <input name="submit" type="submit" value="submit" />
    </form>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Set a flag (called checked) to be false.

    We want a successful outcome with at least one checked checkbox to be true, so whenever we come across a checked checkbox, we set the checked flag to true.

    If at the end of things the checked flag is still false, we know that none of the checkboxes we looked at were checked.

    Code javascript:
    var checked = false;
    for (i=0; i<frm.length; i++)  {
    	if (frm.elements[i].id.indexOf('chkStatus') !=-1)    {
    	  	if (frm.elements[i].checked) {
    			checked = true;
    		}
    	}
    }
    if (!checked) {
    	missinginfo += "\n     -  Select a checkbox";
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that worked! thanks!


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
  •