Stump the Panel Topic: Set a field mandatory with javascript on onChange event http://www.endusersharepoint.com/STP/ SharePoint QA en Fri, 28 Aug 2009 05:18:25 +0000 AutoSponge on "Set a field mandatory with javascript on onChange event" http://www.endusersharepoint.com/STP/topic/set-a-field-mandatory-with-javascript-on-onchange-event#post-980 Sun, 31 Aug 2008 22:56:18 +0000 AutoSponge 980@http://www.endusersharepoint.com/STP/ <p>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:</p> <pre><code>&lt;script type=&quot;text/javascript&quot;&gt; function PreSaveAction() { var findDate = getTagFromIdentifierAndTitle(&quot;input&quot;,&quot;DateTimeFieldDate&quot;,&quot;Due Date&quot;); var findText = getTagFromIdentifierAndTitle(&quot;textarea&quot;,&quot;&quot;,&quot;Test&quot;); var txt = document.createElement(&quot;span&quot;); txt.className = &quot;ms-formvalidation&quot;; if(findDate.value != &quot;&quot; &#38;&#38; findText.value == &quot;&quot;) { txt.appendChild(document.createTextNode(&quot;You must provide a Justification for the Due Date&quot;)); txt.id = &quot;val1&quot;; if (document.getElementById(&quot;val1&quot;)){ }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 &lt; tags.length; i++) { var tempString = tags[i].id; if (tags[i].title == title &#38;&#38; (identifier == &quot;&quot; || tempString.indexOf(identifier) == tempString.length - len)) { return tags[i]; } } return null; } //--&gt; &lt;/script&gt;</code></pre> spykew on "Set a field mandatory with javascript on onChange event" http://www.endusersharepoint.com/STP/topic/set-a-field-mandatory-with-javascript-on-onchange-event#post-952 Thu, 28 Aug 2008 16:34:14 +0000 spykew 952@http://www.endusersharepoint.com/STP/ <p>Hi again,</p> <p>You are totally right, I forgot to mention that on the previous post.</p> <p>Anyway thanks for your help, maybe help others in a similar situation.</p> <p>Cheers,<br /> Roger </p> AutoSponge on "Set a field mandatory with javascript on onChange event" http://www.endusersharepoint.com/STP/topic/set-a-field-mandatory-with-javascript-on-onchange-event#post-950 Thu, 28 Aug 2008 15:15:23 +0000 AutoSponge 950@http://www.endusersharepoint.com/STP/ <p>spykew,</p> <p>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. </p> spykew on "Set a field mandatory with javascript on onChange event" http://www.endusersharepoint.com/STP/topic/set-a-field-mandatory-with-javascript-on-onchange-event#post-949 Thu, 28 Aug 2008 13:06:20 +0000 spykew 949@http://www.endusersharepoint.com/STP/ <p>Hi AutoSponge,</p> <p>Thanks for you help.</p> <p>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#.</p> <p>I just want to know if there's a way of access this functionality by javascript. </p> <p>Thanks,<br /> Roger </p> AutoSponge on "Set a field mandatory with javascript on onChange event" http://www.endusersharepoint.com/STP/topic/set-a-field-mandatory-with-javascript-on-onchange-event#post-948 Thu, 28 Aug 2008 12:50:29 +0000 AutoSponge 948@http://www.endusersharepoint.com/STP/ <p>spykew,</p> <p>This script will only work while Justification is a "Plain Text" field.</p> <pre><code>&lt;script type=&quot;text/javascript&quot;&gt; function PreSaveAction() { var findDate = getTagFromIdentifierAndTitle(&quot;input&quot;,&quot;DateTimeFieldDate&quot;,&quot;Due Date&quot;); var findText = getTagFromIdentifierAndTitle(&quot;textarea&quot;,&quot;&quot;,&quot;Justification&quot;); if(findDate.value != &quot;&quot; &#38;&#38; findText.value == &quot;&quot;) { alert(&quot;You must provide a Justification for the Due Date&quot;); 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 &lt; tags.length; i++) { var tempString = tags[i].id; if (tags[i].title == title &#38;&#38; (identifier == &quot;&quot; || tempString.indexOf(identifier) == tempString.length - len)) { return tags[i]; } } return null; } //--&gt; &lt;/script&gt;</code></pre> spykew on "Set a field mandatory with javascript on onChange event" http://www.endusersharepoint.com/STP/topic/set-a-field-mandatory-with-javascript-on-onchange-event#post-946 Thu, 28 Aug 2008 11:22:31 +0000 spykew 946@http://www.endusersharepoint.com/STP/ <p>Hi,</p> <p>I want to set a field mandatory if another field was selected through javascript.</p> <p>My scenario:<br /> 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.<br /> The due date is not mandatory so I can't set the field "Justification" to always mandatory.</p> <p>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.</p> <p>Any help available?</p> <p>Thanks in advance,<br /> Roger </p>