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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Repeat region validation

    I have a number of dynamically generated drop down menus in an ASP repeat region. The output is:

    myorder1
    myorder2
    myorder3

    and so on. The values are set to match the specific data record value.

    I want to validate these menus so that a user can't select the same value for any one of the drop downs.

    At the moment I'm trying to get the Js to return the values for each instance of the myorder drop down:

    function checkDupes(myform) {

    var i=1
    for (i=1;i<=4;i++)
    {
    document.write(document.myform.myorder+i.selectedIndex
    document.write("<br />")
    }

    }

    I just get a NAN output.

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    If your form is named "myform" and your select element is named "myorder" then you reference it as

    document.myform.myorder.options[document.myform.myorder.selectedIndex].value;

    as a shortcut.

    If you're trying to loop through elements with a name like myorder1, myorder2, etc, you have to use the long way.

    var s = document.myform.elements["myform"+i]; // this is a select element
    alert(s.options[s.selectedIndex].value); // this is the selected value

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Nikki, I will give this a try.

    Got this:
    var i=1
    for (i=1;i<=4;i++)
    {

    var s = document.myform.elements["myorder"+i]; // this is a select element

    if(s == s){
    alert("WARNING DUPLICATE VALUE");
    }else{
    document.write("Success");
    }
    }

    }

    I think I have the conditional statement wrong as it does detect that the value of s = s but that would also include itself.

    I'm trying to get it to detect that select1 is the same as select3 or that they all have different values so it's ok to submit.
    Last edited by Nik36; 12-08-2006 at 10:19 AM.

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Check this out.

    Code:
    <html>
    <script type="text/javascript">
    function checkit(frm)
    {
    	var MAXELEMENTS = 4;
    	
    	for (var i=1; i<MAXELEMENTS; i++)
    	{
    		var value = frm.elements["myorder"+i].options[frm.elements["myorder"+i].selectedIndex].value;
    		for (var j=i+1; j<=MAXELEMENTS; j++)
    		{
    			if (value == frm.elements["myorder"+j].options[frm.elements["myorder"+j].selectedIndex].value)
    			{
    				alert("WARNING DUPLICATE VALUE");
    				return false;
    			}
    			else
    			{
    				alert("Success");
    				return true;
    			}
    		}
    	}
    }
    </script>
    <body>
    <p>test</p>
    <form action="" method="get" onsubmit="return checkit(this)">
      <p>
        <select name="myorder1">
          <option value="foo">foo</option>
          <option value="bar">bar</option>
          <option value="wow">wow</option>
          <option value="bugs">bugs</option>
          <option value="bunny">bunny</option>
        </select>
      </p>
      <p>      <select name="myorder2">
          <option value="foo">foo</option>
          <option value="bar">bar</option>
          <option value="wow">wow</option>
          <option value="bugs">bugs</option>
          <option value="bunny">bunny</option>
              </select>
      </p>
      <p>    <select name="myorder3">
          <option value="foo">foo</option>
          <option value="bar">bar</option>
          <option value="wow">wow</option>
          <option value="bugs">bugs</option>
          <option value="bunny">bunny</option>
                    </select>
    </p>
      <p>
        <select name="myorder4">
          <option value="foo">foo</option>
          <option value="bar">bar</option>
          <option value="wow">wow</option>
          <option value="bugs">bugs</option>
          <option value="bunny">bunny</option>
        </select>
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit"> 
      </p>
    </form>
    </body>
    </html>

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

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