PDA

View Full Version : Required field on menu select



nate51
08-31-2011, 09:02 PM
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...

<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...

<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.

vwphillips
09-01-2011, 08:35 AM
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

nate51
09-01-2011, 01:12 PM
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.