4

I need to be able to validate fractions on my form using jquery.validate and jquery.validate.unobtrusive. I have changed my html markup to include 'fraction'

<input class="span12 data fraction" data-val="true" name="FrontHoodHeightL" id="FrontHoodHeightL" type="text" value="29 15/16">

in the class name and I have included the following javascript in my page.

$.validator.addMethod("fraction", function (value, element, param) {
    var fracExp = new RegExp("(?:-(?!0))?\d+(?:(?: \d+)?/\d+)?");
    return fracExp.test(value);
});

but the method never seems to get called. What step am I missing to wire this up?

PlTaylor
  • 7,345
  • 11
  • 52
  • 94

1 Answers1

4

One more step: Use addClassRules to add a class rule that uses your new method:

$.validator.addClassRules("fraction", {
    fraction: true
});

Example: http://jsfiddle.net/X2m4U/

Note: It looks like you are trying to use negative lookbehinds in your regex. This won't work with JavaScript.

Community
  • 1
  • 1
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • Thanks. Can you point me in the direction of the documentation where you found this? I looked for a while and couldn't find anything. You are also correct about the regex statement. I'll have to figure out a different one that will work. – PlTaylor Jun 22 '12 at 10:51
  • @PlTaylor: I've added a link to the documentation for `addClassRules`. I am not sure there is explicit documentation that walks you through adding a rule like this, I'm just pretty familiar with the plugin. – Andrew Whitaker Jun 22 '12 at 12:26