0

In a SharePoint 2010 form, a Rich Text field is rendered not as a form element, but as a div. I need to use jQuery Validator Plugin to check the contents of this div.

<form>

  <!-- this will validate -->
  <select class="check_this">
    <option>Testing form element</option>
  </select>

  <!-- this will not validate -->
  <div class="check_this">
    Testing div
  </div>

</form>
Sparky
  • 98,165
  • 25
  • 199
  • 285
Jonathan Eckman
  • 2,071
  • 3
  • 23
  • 49

1 Answers1

1

The problem is that non-form elements are not tested against, right?

I haven't tested this approach but why don't you render an empty select-element that has the same name as the id of div and add a method that grabs the correct element:

HTML:

<form>
    <!-- this is a dummy element -->
    <select name="myElem">
        <option>Testing form element</option>
    </select>

    <!-- this is my actual element -->
    <div id="myElem">
        Testing div
    </div>
</form>

JS:

jQuery.validator.addMethod("myForm", function(value, element) { 
    var strId = $(element).attr('name');
    var myActualElement = $('form').find('#' + strId);
    var myActualContent = myActualElement.text();

    return this.optional(element) || /regexwhateveryouwant/.test(myActualContent); 
}, "Custom error message");

If you're not able to render a dummy select-element, you need to create it with jQuery.

nirazul
  • 3,928
  • 4
  • 26
  • 46
  • I may be able to add the contents of the div to a hidden dummy ` – Jonathan Eckman Nov 28 '12 at 20:43
  • Can you confirm that non-form elements are not tested against? I cannot find this in the documentation. – Jonathan Eckman Nov 28 '12 at 21:25
  • There are plenty of questions on StackOverflow. For example: [this one](http://stackoverflow.com/questions/6617642/jquery-validator-how-to-validate-against-non-form-items)... However I haven't found a recent one (from 2012). Also the whole documentation never mentions an example validating a non-form field. – nirazul Nov 28 '12 at 22:41