Forums

Forums / Developing Portals / Can you set a field to required using javascript?

Can you set a field to required using javascript?

Thread is closed for posting
2 posts, 1 answered
  1. geoff.white
    geoff.white avatar
    31 posts
    Registered:
    27 Jan 2015
    12 Dec 2016
    Link to this post
    Can you set a field to required using javascript?

    I want to set fields to be required based on the value of another field.
  2. Clinton Bale
    Clinton Bale avatar
    126 posts
    Registered:
    21 Feb 2014
    Answered
    03 Jan 2017 in reply to geoff.white
    Link to this post
    Hello Geoff,

    Here is some sample code we have used in the past to enable/disable required field validation on certain widgets:

    function setRequired(controlId, isRequired) {
      var control;
      if(controlId&&(control=findControl(controlId))) {  
        ValidatorEnable($(control.get_element()).find("span[id$=requiredFieldValidator]").eq(0), isRequired)
      }
    }
    
    // makes CrmLookup_C001 required
    setRequired("CrmLookup_C001", true);
    
    // makes CrmLookup_C001 optional
    setRequired("CrmLookup_C001", false);
    

    If you are having any other problems please let me know.

    Best Regards,
    Clinton
2 posts, 1 answered