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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Form Validation - Checkboxes

    Hi, I'm pretty new to JavaScript and I have a few questions regarding form validation, esp. checkboxes:

    1. The system I'm working on has a screen where the user is required to select exactly 2 checkboxes out of 5. These checkboxes correspond to records pulled from a database. These threads have been of some help but I guess I don't know enough to modify them to make it work for my situation:

    http://www.codingforums.com/showthre...threadid=16608

    http://www.codingforums.com/showthre...&threadid=8558

    Right now the page only checks for the maximum number ( > 2) and displays an alert. I just need help making sure the minimum checked is 2 records. I'm thinking this check has to be on the button that takes the user to the next page?

    2. If this is completed successfully (2 records checked), the user will go to a screen that requires that they input some information into 2 text boxes and click a button to do the same for the second record checked from the previous screen. However, if the information is unavailable, they can click a checkbox marked "Unavailable". What I want to do is display a yes/no? box if they check the box that warns them that any information entered in the text box will not be recorded to the database. The message box will either allow them to continue to the next record or uncheck the box and let them enter the data in the text box.

    Hopefully this makes sense. If anyone is willing to help on either of these I can send you a link to the site I have so far. It requires a password and I'd rather not give it out on here, although it is all dummy data at this time. Thanks!

  • #2
    New Coder
    Join Date
    Mar 2003
    Location
    Somewhere far beyond
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    // insert this functions into <head> part
    function checkForm(frm)
    {
    	var iChecked=countChecked(frm);
    	if (iChecked==iBoxmax) return true;
    	alert("You have to select exactly "+iBoxmax+" records!");
    	return false;
    }
    
    function countChecked(frm)
    {
    	var oEl,e=0, iChecked=0;
    	while (oEl = frm.elements[e++]) if (oEl.type == 'checkbox' && oEl.checked) ++iChecked;
    	return iChecked;
    }
    ....
    <FORM name='frmChecker' action='checker2.asp' method='post' onsubmit='return checkForm(this)'>
    WBR, Weirdan.

  • #3
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Weirdan, Thanks man! That's exactly what I was looking for.

  • #4
    New Coder
    Join Date
    Mar 2003
    Location
    Somewhere far beyond
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function isOK(form) {
    	if (isValid(form.q1.value) == false) {
    		alert("Please enter the appropriate quantity.");
    		form.q1.focus();
    		return false;
    	}
    
    	if (isValid(form.q2.value) == false) {
    		alert("Please enter the appropriate quantity.");
    		form.q2.focus();
    		return false;
    	}
    	if (form.check1.value)
    		   return confirm("Do you want to abandon your changes?\nIf you'll click OK changes will not be reflected in database");
    	return true;
    }
    This code for checker2.asp
    WBR, Weirdan.

  • #5
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That does what I want but there are three exceptions: I think its just adding an if statement but my logic isn't working today.

    1. If the user enters a number in both text boxes, no message should be displayed and they should go to the next record.

    2. If the user only checks "Unavailable" with no quantities entered, then the same ok box should be displayed or something that say's "Are you sure?"

    3. If the user enters numbers in either or both boxes and checks unavailable, if they click ok to proceed, can i make the values of each text box = ""?

    Thanks for all your help so far. I really appreciate it.

  • #6
    New Coder
    Join Date
    Mar 2003
    Location
    Somewhere far beyond
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function isOK(form) {
       if (isValid(form.q1.value) == false) {
             alert("Please enter the appropriate quantity.");
             form.q1.focus();
             return false;
       }
       if (isValid(form.q2.value) == false) {
            alert("Please enter the appropriate quantity.");
            form.q2.focus();
            return false;
       }
       if (form.check1.value) {
          var abandon=confirm("Do you want to abandon your changes?\nIf you'll click OK changes will not be reflected in database");
          if(abandon)  form.q1.value=form.q2.value="";
          return abandon;
       }
          return true;
    }
    Also you can check if "Unavailable" checkbox set serverside. Something like this:
    PHP Code:
    if($_GET['check1'])
      
    // ignore data in q1 and q2 fields. 
    WBR, Weirdan.

  • #7
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Weirdan, I'm still having trouble with the checkbox, it seems like this statement is always true:

    if (form.check1.value) {

    could I just have a confirm pop up right when they check the box and then submit the form if they click ok?

    Thanks

    Ok I think I got it now:

    function isOK(form) {

    var formLength = form.length;
    for (var e=0; e<formLength; ++e)
    if (form.elements[e].type == 'checkbox' & form.elements[e].checked) {
    var abandon=confirm("Are you sure you want to continue?\nIf you press OK, no quantities will be submitted.");
    if(abandon) form.q1.value=form.q2.value="";
    return abandon;
    }

    if (isValid(form.q1.value) == false || validateNmbr(form.q1.value) == false) {
    alert("Please enter the appropriate quantity for this field.");
    form.q1.focus();
    return false;
    }

    if (isValid(form.q2.value) == false || validateNmbr(form.q2.value) == false) {
    alert("Please enter the appropriate quantity for this field.");
    form.q2.focus();
    return false;
    }
    return true;
    }

    Thanks for all your help on this!!!
    Last edited by Shove IT; 04-14-2003 at 03:38 PM.

  • #8
    New Coder
    Join Date
    Mar 2003
    Location
    Somewhere far beyond
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function isOK(form) {
         var formLength = form.length;
          for (var e=0; e<formLength; ++e)
              if (form.elements[e].type == 'checkbox' & form.elements[e].checked) 
             {
                 var abandon=confirm("Are you sure you want to continue?\nIf you press OK, no quantities will be submitted.");
                 if(abandon) form.q1.value=form.q2.value="";
                 return abandon;
             }
    //.....
    It will work for you if you have only 1 checkbox in the form.
    Edit: If you have unavailable checkbox first, I mean

    Code:
    if (form.check1.value) {
    What browser do you use? Such a code works quite well in ie6 (I haven't checked it in other browsers).
    Last edited by Weirdan; 04-15-2003 at 09:54 AM.
    WBR, Weirdan.

  • #9
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm using IE6 also. I think what I have is working so I'll probably leave it since we have to install it later this week !

    Do you know if the "form.check1.value" will check if the box is checked or just check if there is a value assigned to it? It seems like when I run that line of code the value is always 1, which is what I assigned to it in the HTML. I'll look into it. Thanks again.


  •  

    Posting Permissions

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