2

Is it possible to make a field required due to the selection of a previous field?

So, if I have a dropdown with options (a, b, c, d) and if the user selects "d" then notes are required, but if they choose any other no notes are required.

How would I go about this?

3 Answers 3

3

I did it recently with jQuery, http://endusersharepoint.com/STP/viewtopic.php?f=13&t=2916&start=10

This is what the code looks like in my scenario, something could be adapted for your use.

<script src="../../Resources/jquery.js" type="text/javascript"></script>
<script src="../../Resources/SPServices.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var text = $("input[title$='VenLookup']").val();
var text2 = $("select[title$='VenLookup'] :selected").text();
  if(text != "Other - Not Listed" || text2 != "Other - Not Listed")
   {
   $(".VendorDetails").hide();
   }
//For IE
$("input[title$='VenLookup']").change(function() {
  var inputTextVal = $("input[title$='VenLookup']").val();
    if(inputTextVal == "Other - Not Listed")
   {
      alert('Please fill out all the vendor fields that will appear on the form');
      $(".VendorDetails").show();
      $(".VendorLabel").append("<span class='ms-formvalidation'>&nbsp;*</span>");      
   }
   else {}
  });
// For Firefox
$("select[title$='VenLookup']").change(function() {
  var selectTextVal = $("select[title$='VenLookup'] :selected").text();
   if(selectTextVal == "Other - Not Listed")
   {
      alert('Please fill out all the vendor fields that will appear on the form');
      $(".VendorDetails").show();
      $(".VendorLabel").append("<span class='ms-formvalidation'>&nbsp;*</span>");   
   }
   else {}
});
function PreSaveAction() {
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Labs",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='Lab_x0020_Location'/><Value Type='Text'>" + $("input[title$='Lab']").val() + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
   $("input[title='Lab ID']").val($(this).attr("ows_LabID"));
    }); //close each
  } //close complete
}); //close SPServices


var Intext = $("input[title$='VenLookup']").val();
var Seltext = $("select[title$='VenLookup'] :selected").text();
  if(Intext != "Other - Not Listed" || Seltext != "Other - Not Listed")
{
$("tr.VendorDetails input").each(function() {
    error = false;
    if ($(this).val().length <= 0) {
     error = true;
    }
  });
  if (error) 
  {
   alert("Please fill out all required fields");  
    return false;
  } 
  else 
  {
    return true;
  }
}

} // close presave  
}); //end doc ready

</script>
6
  • Link now broken, can you please update? Also if you can give us a brief summary of the article that would be perfect. Thanks!
    – Alex Angas
    Commented Jun 27, 2011 at 3:47
  • Blast, Mark took the site from read ony to down. I'll find my code and edit my post. Commented Jun 27, 2011 at 12:02
  • He hasn't moved the content somewhere else by any chance?
    – Alex Angas
    Commented Jun 28, 2011 at 0:59
  • I thought he was keeping it as a read only archive since there was a lot of good content in the forums. Commented Jun 28, 2011 at 12:05
  • Found the code and edited my post if you were interested Alex. Commented Jul 6, 2011 at 12:01
0

This cannot be done OOB. Use a custom form/infopath to achieve this functionality.

0

Either you develop your own custom field or you have modify your add and edit pages and add a bit of JavaScript/jquery to do this check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.