Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Array[] Javascript Validation Help Needed

    Hi,

    I have a .php page that have a multiple check box question that requires validation whether or not the user checked any values.

    -----------------------------
    HTML:
    -----------------------------
    <p><font color="#FF0000">*</font> If you are a health professional, what is your practice setting (check all that apply):<br>
    <input name="Q2_PracticeSetting[]" type="checkbox" value="Hospital">
    Hospital<br>
    <input name="Q2_PracticeSetting[]" type="checkbox" value="Outpatient setting">
    Outpatient setting<br>
    <input name="Q2_PracticeSetting[]" type="checkbox" value="Academia">
    Academia<br>
    <input name="Q2_PracticeSetting[]" type="checkbox" value="Research">
    Research<br>
    <input name="Q2_PracticeSetting[]" type="checkbox" value="Other">
    Other
    </p>

    -----------------------------
    Using Javascript:
    -----------------------------
    function ValidateForm() {

    field = document.register.firstname;
    if (isBlank(field, "First Name")) return false;

    field = document.register.lastname;
    if (isBlank(field, "Last Name")) return false;

    field = document.register.credential;
    if (isBlank(field, "Credentials")) return false;

    field = document.register.email;
    if (isBlank(field, "Email Address")) return false;
    if (!isEmail(field, "Email Address")) return false;

    var chks = document.register.elements['Q2_PracticeSetting[]'];
    var hasChecked = true;
    for (var i=0;i<chks.length;i++){
    if (chks[i].checked){
    hasChecked = false;
    break;
    }
    }
    if (!hasChecked){
    alert("Please select at least one.");
    chks[0].focus();
    return false;
    }


    return true;
    }

    -----------------------------
    Result:
    -----------------------------
    Not working

    Any help is much apprciated!

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    It looks like you've got it backwards
    Code:
    var hasChecked = false;
    for (var i = 0; i < chks.length; i++)
    {
    	if (chks[i].checked)
    	{
    		hasChecked = true;
    		break;
    	}
    }
    Try that.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #3
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    JavaScript doesn't like the [] as part of the input name. But of course you need that for your php array.....

    Solution? Add id="Q2_PracticeSetting" to the checkbox fields, and javascript will happily use that, while php will continue to pull the array name properly.

  • #4
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by sonjay
    Add id="Q2_PracticeSetting" to the checkbox fields, and javascript will happily use that, while php will continue to pull the array name properly.
    id values must be unique.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>57321</title>
    		<meta name="description" content="Validate multiple input elements">
    	</head>
    	<body>
    		<form action="57321" method="get" onsubmit="return validateForm()">
    			<fieldset>
    				<legend>If you are a health professional, what is your practice setting (check all that apply)</legend>
    				<div>
    					<input id="hospital" name="Q2_PracticeSetting[]" type="checkbox" value="Hospital">
    					<label for="hospital">Hospital</label>
    				</div>
    				<div>
    					<input id="outpatient" name="Q2_PracticeSetting[]" type="checkbox" value="Outpatient setting">
    					<label for="outpatient">Outpatient setting</label>
    				</div>
    				<div>
    					<input id="academia" name="Q2_PracticeSetting[]" type="checkbox" value="Academia">
    					<label for="academia">Academia</label>
    				</div>
    				<div>
    					<input id="research" name="Q2_PracticeSetting[]" type="checkbox" value="Research">
    					<label for="research">Research</label>
    				</div>
    				<div>
    					<input id="other" name="Q2_PracticeSetting[]" type="checkbox" value="Other">
    					<label for="other">Other</label>
    				</div>
    				<input type="submit">
    			</fieldset>
    		</form>
    
    		<script type="text/javascript">
    			function validateForm()
    			{
    				var chks = document.getElementsByName('Q2_PracticeSetting[]');
    				var hasChecked = false;
    				for (var i = 0; i < chks.length; i++)
    				{
    					if (chks[i].checked)
    					{
    						hasChecked = true;
    						break;
    					}
    				}
    				if (!hasChecked)
    				{
    					alert("Please select at least one.");
    					chks[0].focus();
    					return false;
    				}
    				return true;
    			}
    		</script>
    	</body>
    </html>
    Last edited by hemebond; 04-20-2005 at 02:24 AM.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #5
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks to sonjay and hemebond!!! it helps!!


  •  

    Posting Permissions

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