Announcement

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

    How to validate DateTimeItem data (if user enters wrong syntax for date or time)?

    [We use the Pro license of SmartGWT, but I think this is a client-side problem]

    I use DateTimeItem to allow the user to enter date and time. I use a button click event and then read the data. This works, if the user enters correct data for date and time.

    However, if the user uses textfield to enter data and stops in the middle, nothing happens after clicking a button (also no exception in the stacktrace). What I need is an error message (popup).

    I do NOT want to do any semantic validation (e.g. any computations or test if date is lower / higher than anything), just syntax validation of date and time.

    Here is the code:

    DateTimeItem baselineDateTimeItem = new DateTimeItem("baselineDate", "Baseline Date");
    baselineDateTimeItem.setUseTextField(true);
    baselineDateTimeItem.setUseMask(true);
    baselineDateTimeItem.setDisplayFormat(DateDisplayFormat.TOEUROPEANSHORTDATETIME);

    #### on Button Click: #####
    baselineDate = baselineDateTimeItem.getValueAsDate();

    if (! baselineDateTimeItem.validate()) {
    SC.warn("You must enter a correct date and time for 'Baseline Date'.");
    }


    How can I catch an exception (i.e. the user did not enter the whole date and time correctly)? I want to give the user some feedback that the entered data is wrong. At the moment nothing happens when the user clicks the button after entering wrong data.

    I tried to use dateTimeItem.validate(), but this method is not working. Unfortunately, the Javadoc does not tell me what this method does? Is this the right method? Or how do I solve the problem?

    Thank you...

    #2
    How about this:

    try{
    date = retrieve date..
    }
    catch(Exception e){
    // Inform incorrect date
    }

    Comment


      #3
      That works. Thank you. I was not aware that there is a Java Exception thrown (no stacktrace)...

      However, just for understandings: Can someone explain why there is a validate()-method? What does it do?

      Comment

      Working...
      X