Results 1 to 3 of 3

Thread: Required field on menu select

  1. #1
    Join Date
    Dec 2007
    Location
    Mississauga
    Posts
    166
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Required field on menu select

    I have a form that when you select 'Yes' from a drop down menu a field appears. The projects requires that I make the field mandatory if the option of yes is selected fro the drop down.

    I am using this as my Javascript validation...
    Code:
    <script language="JavaScript"> 
    <!--
    function formCheck(formobj){
    	// Enter name of mandatory fields
    	var fieldRequired = Array("firstname", "lastname", "email", "homephone", "mailingaddress", "city", "province", "postalcode", "country", "Condo Type", "Price Range", "Agent");
    	// Enter field description to appear in the dialog box
    	var fieldDescription = Array("First Name", "Last Name", "Email", "Phone", "Address", "City", "State or Province", "Zip or Postal", "Country", "Condo Type", "Price Range", "Agent");
    	// dialog message
    	var alertMsg = "Please complete the following fields:\n";
    	
    	var l_Msg = alertMsg.length;
    	
    	for (var i = 0; i < fieldRequired.length; i++){
    		var obj = formobj.elements[fieldRequired[i]];
    		if (obj){
    			switch(obj.type){
    			case "select-one":
    				if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == ""){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "select-multiple":
    				if (obj.selectedIndex == -1){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "text":
    			case "textarea":
    				if (obj.value == "" || obj.value == null){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			default:
    			}
    			if (obj.type == undefined){
    				var blnchecked = false;
    				for (var j = 0; j < obj.length; j++){
    					if (obj[j].checked){
    						blnchecked = true;
    					}
    				}
    				if (!blnchecked){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    			}
    		}
    	}
    
    	if (formobj.Agent.selectedIndex == 1)
    	{
    		formobj.listid.value = "61785";
    	}
    	else if (formobj.Agent.selectedIndex == 2)
    	{
    		formobj.listid.value = "61741,61744";
    	}
    
    	if (alertMsg.length == l_Msg){
    		return true;
    	}else{
    		alert(alertMsg);
    		return false;
    	}
    }
    // -->
    </script>
    And this to make the field appear...
    Code:
    <script type="text/javascript">
    var LocnInfo = {
    	'Prospect':'',
        'Broker':'Brokerage Name*: <input type="text" name="brokerage" id="brokerage" />'
    }
    function UpdateLocation(info) {
      document.getElementById('locnMsg').innerHTML = LocnInfo[info];
    }
    </script>
    I am not a Java person by anymeans so I don't even know where to start, but is it simple enough to have some type of 'if' command in the validation that look for the fields visability?

    Pointers are great but if you know of any tutorials or step by steps, that would be great. Telling me it's possible still leaves me stumped.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    as I understand a new field is added to the page

    if so the field name can be added to the required fields array

    or post a link to your page
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Dec 2007
    Location
    Mississauga
    Posts
    166
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by vwphillips View Post
    as I understand a new field is added to the page

    if so the field name can be added to the required fields array

    or post a link to your page
    @vwphillips Thanks for the info, I tried it again and realised I had made a spelling error the first time I did it. I added the field name to the array and it works great, when the field is on screen and not filled out it gets a validation warning, when the option is not selected to make the new field appear it ignores it.
    Last edited by nate51; 09-01-2011 at 05:39 PM. Reason: Adding worked

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
  •