Opened 11 years ago

Closed 11 years ago

Last modified 9 years ago

#7004 closed defect (fixed)

regexp backreference doesn't work right in ValidationTextBox

Reported by: aaronr@… Owned by: Douglas Hays
Priority: high Milestone: 1.2
Component: Dijit - Form Version: 1.0
Keywords: Cc:
Blocked By: Blocking:

Description (last modified by bill)

I took a regular expression documented on the web as good for dates and tried it in a dojo ValidationTextBox? and it did not work well. Dojo does not seem to like a backreference much. But if I test a date against the pattern in Javascript, it returns 'true' so I believe that the expression is good.

Here is my testcase. Note that you'll have to fix up the dojo paths when you try it out.

<html>
  <head>
    <title>Dojo: Hello World!</title>
    <!-- SECTION 1 -->
    <style type="text/css">
        @import "http://b2aaaron.austin.ibm.com/dojo-1.0.2/dijit/themes/tundra/tundra.css";
        @import "http://b2aaaron.austin.ibm.com/dojo-1.0.2/dojo/resources/dojo.css"
    </style>
    <script type="text/javascript" src="http://b2aaaron.austin.ibm.com/dojo-1.0.2/dojo/dojo.js"
      djConfig="parseOnLoad: true"></script>
    <!-- SECTION 2 -->
    <script type="text/javascript">
      // Load Dojo's code relating to the Button widget
      dojo.require("dijit.form.ValidationTextBox");
      dojo.require("dojo.regexp");
    </script>
    <script>
      dojo.addOnLoad(function(){
        var input = document.createElement('input');
        input.id = 'jsDijit';
        input.value = '3';
        document.body.appendChild(input);
        var control = new dijit.form.ValidationTextBox({
              value: "2008-12-12",
              regExp: "(19|20)\\d\\d([- /.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])",
              trim: true,
              invalidMessage: "value must be a date of pattern yyyy/mm/yy"
        }, input);
        var testValue = "2008-12-12";
        var pattern = /(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])/g;
        var testResult = pattern.test(testValue);
        var jsResult = document.createElement('span');
        jsResult.innerHTML = "<br/>"+testValue+" matches the regular expression: "+testResult;
        document.body.appendChild(jsResult);
      });
    </script>
  </head>
  <body class="tundra">
      <h2> This should show two 'valid' ValidationTextBoxes </h2>
      <input id="goodinput" dojotype="dijit.form.ValidationTextBox"
             regExp="(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])" trim="true" invalidMessage="value must be a date of pattern yyyy/mm/yy" value="2008-12-12">
      </input>
  </body>
</html>

The problem seems to be with (19|20)\d\d...if I replace that with \d{4}, then the text boxes seem to be ok with that.

If you have any questions, feel free to contact me at aaronr at us.ibm.com

Change History (8)

comment:1 Changed 11 years ago by bill

Description: modified (diff)
Reporter: changed from guest to aaronr@…

BTW you can attach testcases w/the attach file button (that's preferred).

I'm not sure why you are using backreferences though. It seems like you can't access the data from the backreferences anyway, right?

comment:2 Changed 11 years ago by guest

Sorry I was late in responding, been out on vacation.

The reason for using the backreference was to ensure that the date separators were the same. Without the backreference, 2008/12-12 would be valid. With the back reference, then 2008/12/12 or 2008-12-12 would be enforced.

--Aaron

comment:3 Changed 11 years ago by bill

Milestone: 1.4
Owner: set to Douglas Hays

Oh I see, makes sense.

comment:4 Changed 11 years ago by Douglas Hays

Milestone: 1.41.2
Resolution: invalid
Status: newclosed

Every ( in a regexp increments the backreference count. (19|20) is \1. Change it to (?:19|20) in order to not create a backreference.

comment:5 Changed 11 years ago by aaronreed

Resolution: invalid
Status: closedreopened

I don't know if we are on the same page. I'm trying to get at the value represented by ([- /.]) which should be \2 (which is what I'm trying to use without success). I don't see how what I'm doing is incorrect but if it is, I'd love to know why. And since the rule seems to work fine in JS, I think I've got it right.

comment:6 Changed 11 years ago by Douglas Hays

The problem is that the validation code adds (...)$ around the user's re. So
2 needs to be
3 and then it'll work, or (19|20) could be changed to (?:19|20) and it'll work.
I'm going to change
(...)$ to (?:...)$ so that this test will work, but existing users who have figured out the extra backreference will now be broken.

comment:7 Changed 11 years ago by Douglas Hays

Resolution: fixed
Status: reopenedclosed

(In [14315]) Fixes #7004. Add ?: so that user backreferences work as expected.

comment:8 Changed 9 years ago by bill

Component: DijitDijit - Form
Note: See TracTickets for help on using tickets.