Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    RegExpValidator and DateItem

    I'm using a DateItem with setUseTextField set to true. I'm using European date format which is "dd.MM.yyyy".
    That is why I set the date formater at the beginning of my form like this:
    Code:
            DateDisplayFormatter dateFormater = new DateDisplayFormatter() {
                @Override
                public String format(Date date) {
                    if (date == null) return null;
                    DateTimeFormat dateFormatter = DateTimeFormat.getFormat("dd.MM.yyyy");
                    return dateFormatter.format(date);
                }
            };
            DateUtil.setShortDateDisplayFormatter(dateFormater);
            DateUtil.setDateInputFormatter(new DateInputFormatter()
            {
                @Override
                public Date parse(String dateString) {
                    final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("dd.MM.yyyy");
                    Date date = dateFormatter.parse(dateString);
                    return date;
                }            
            });
    Now I also want to use a validator to check the input of the text field, so I created a RegExValidator. But the validator reports an invalid value even if a valid date is entered. I checked the regular expression and it should be fine.
    This is my code for the DateItem:
    Code:
    DateItem birthDateDI = new DateItem("birthdate");
            birthDateDI.setUseTextField(true);
            birthDateDI.setValidators(new RegExpValidator("^(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.(19|20)\\d\\d$"));
            birthDateDI.setValidateOnExit(true);
    Entering for example "27.10.2010" into the text box or select a date with the calendar, results in the validator claiming the date entered is invalid.
    What am I doing wrong here?

    #2
    Set the date input format to "DMY", and note also you don't need to set a custom function for this type of display, it's one of the default formats (note the separator char is settable).

    Comment


      #3
      Thanks Isomorphic for your reply, but I don't get it. Where should I set the date format to DMY and the seperator char?
      The only function I could find was setMaskDateSeparator(), but that had no effect.
      I removed the whole lines with DateUtil functions and used this code
      Code:
              birthDateDI.setUseTextField(true);
              birthDateDI.setMaskDateSeparator(".");
             birthDateDI.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
              birthDateDI.setValidateOnExit(true);
      But still the date is shown with "/" as seperator instead of "." (but the validator seems to work fine now).

      Comment


        #4
        All the relevant APIs are on DateUtil. The API to set the separator char is recently added however, so you'd need a nightly (smartclient.com/builds).

        Comment


          #5
          OK, thanks. Will try that...

          Comment

          Working...
          X