Stump the Panel » Site Managers and Site Collection Managers

Set a field mandatory with javascript on onChange event

(6 posts)
  • Started 12 months ago by spykew
  • Latest reply from AutoSponge
  1. spykew
    Member

    Hi,

    I want to set a field mandatory if another field was selected through javascript.

    My scenario:
    I have a task list with a due date. I want to ensure that if that due date is filled by the user he must fill a justification text field.
    The due date is not mandatory so I can't set the field "Justification" to always mandatory.

    I'm already using a webpart with javascript code in it to hide some fields on EditForm.aspx page, I think I can use a similar approach.

    Any help available?

    Thanks in advance,
    Roger

    Posted 12 months ago #
  2. spykew,

    This script will only work while Justification is a "Plain Text" field.

    <script type="text/javascript">
    
    function PreSaveAction()
    {
        var findDate = getTagFromIdentifierAndTitle("input","DateTimeFieldDate","Due Date");
        var findText = getTagFromIdentifierAndTitle("textarea","","Justification");
        if(findDate.value != "" && findText.value == "")
        {
            alert("You must provide a Justification for the Due Date");
            return false; // Cancel the item save process
        }
        return true;  // OK to proceed with the save item
    }
    
    function getTagFromIdentifierAndTitle(tagName, identifier, title) {
      var len = identifier.length;
      var tags = document.getElementsByTagName(tagName);
      for (var i=0; i < tags.length; i++) {
        var tempString = tags[i].id;
        if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
          return tags[i];
        }
      }
      return null;
    }
    //-->
    </script>
    Posted 12 months ago #
  3. spykew
    Member

    Hi AutoSponge,

    Thanks for you help.

    I have a similar script that does the same thing, but I want to avoid that kind of notification through javascript windows. I would like to use the sharepoint built in validation like "throw new SPFieldValidationException("Exception Text");" in C#.

    I just want to know if there's a way of access this functionality by javascript.

    Thanks,
    Roger

    Posted 12 months ago #
  4. spykew,

    I'm sorry. When you said "I think I can use a similar approach." I thought you meant you wanted to use js. I'm not a developer, so I don't know C#. Good luck.

    Posted 12 months ago #
  5. spykew
    Member

    Hi again,

    You are totally right, I forgot to mention that on the previous post.

    Anyway thanks for your help, maybe help others in a similar situation.

    Cheers,
    Roger

    Posted 12 months ago #
  6. This may not use C# but it looks more like the validation done on the MOSS forms because it uses a new span above the text box instead of an alert pop-up:

    <script type="text/javascript">
    
    function PreSaveAction()
    {
        var findDate = getTagFromIdentifierAndTitle("input","DateTimeFieldDate","Due Date");
        var findText = getTagFromIdentifierAndTitle("textarea","","Test");
        var txt = document.createElement("span");
        txt.className = "ms-formvalidation";
    
        if(findDate.value != "" && findText.value == "")
        {
            txt.appendChild(document.createTextNode("You must provide a Justification for the Due Date"));
            txt.id = "val1";
            if (document.getElementById("val1")){
            }else{
                findText.parentNode.insertBefore(txt, findText);
            }
            return false; // Cancel the item save process
        }
        return true;  // OK to proceed with the save item
    }
    
    function getTagFromIdentifierAndTitle(tagName, identifier, title) {
      var len = identifier.length;
      var tags = document.getElementsByTagName(tagName);
      for (var i=0; i < tags.length; i++) {
        var tempString = tags[i].id;
        if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
          return tags[i];
        }
      }
      return null;
    }
    //-->
    </script>
    Posted 11 months ago #

RSS feed for this topic

Reply

You must log in to post.