Hitachi Vantara Pentaho Community Forums
Results 1 to 8 of 8

Thread: How to validate date using Javascript.

  1. #1

    Default How to validate date using Javascript.

    Hi,

    Is it possible to validate date in specified format using Javascript ?

    Thanks,
    g_nish

  2. #2
    Join Date
    Apr 2016
    Posts
    156

    Default

    Folks on the forum may (rightfully) suggest looking at other PDI Steps to help with date validation before trying JavaScript Step.

    Can you update your post to help describe what you mean by "validate date"? Are you, for example, reading a random string and want to test if it meets a certain date pattern? What are some example inputs/outputs?

    For most date logic: if you can read/input stuff into the "Date" metadata format, then you can use a lot of existing Steps to perform various logic on it.

    If you are aware of the trade-offs of using the JavaScript step and still want to explore what's available... then I suggest looking first at the various Transform functions available in the step.
    * on left window in JavaScript step, expand the folder for 'Transform Functions' above the list of input fields.
    * in the expanded folder, expand each set of functions for data types you're using (e.g. expanding Date Functions)
    * right click each function and select 'Sample'
    * PDI will open a hidden script sample that shows some example code

    Screenshot of this stuff in JavaScript Step:
    Name:  2016-07-25_13-21-06.jpg
Views: 597
Size:  23.0 KB
    Last edited by gutlez; 07-25-2016 at 01:57 PM. Reason: JavaScript not Java. Different programming languages.
    My runtime environment: MacOS, JDK 1.8u121, PDI 7.0

  3. #3

    Default

    Hi Adamnyc,

    Thanks for your reply.

    I need to validate date which is expected to be in format yyyy-mm-dd, if the date is not a valid date then I need to make it <null> using javascript.

    Thanks,
    g_nish

  4. #4
    Join Date
    Jun 2016
    Posts
    107

    Default

    Hi Nishank,

    JavaScript step is not efficient step in kettle, so you can use data validator step.
    Last edited by gutlez; 07-26-2016 at 12:25 PM. Reason: JavaScript not Java. Different programming languages.

  5. #5
    Join Date
    Apr 2016
    Posts
    156

    Default

    Quote Originally Posted by Nishank Goel View Post
    I need to validate date which is expected to be in format yyyy-mm-dd, if the date is not a valid date then I need to make it <null> using javascript.
    Some more follow-up questions:

    * What is the data type of your input? Is it a string/text column you wish to validate matches that pattern?

    * What is the data type of your expected output? Do you wish to make it a string/text for output, do you wish to make it a Date format?

    * Why do you feel you need to use Javascript in this case? What other options have you explored?

    As mentioned previously, the Data Validator step can help you with this a bit more easily. Check out the concept of 'Regular Expressions' if you haven't already, and see how you can use this in the Data Validator step.
    My runtime environment: MacOS, JDK 1.8u121, PDI 7.0

  6. #6
    Join Date
    Apr 2008
    Posts
    4,696

    Default

    Or even more simply:

    Incoming Stream -> Select Values (Change MetaData) -> Outgoing Stream

    On Error from Select Values, send to Set Field Value (set the field to null) and then redirect it back into the Outgoing Steam.

    This makes it clear that you are trying to:
    1) Change the incoming String into a Date with format yyyy-mm-dd and
    2) If not a valid date (eg. Feb 31) or is not in the format specified (eg. date comes in with mm-dd-yyyy), set it to null

  7. #7

    Default

    Hi Nishank,

    You can use this in Modified Javascript Step.

    var sdf = new java.text.SimpleDateFormat("yyyy/MM/dd");
    var Date = sdf.format(YourDate);

    Thanks
    Chinmaya

  8. #8
    Join Date
    Apr 2008
    Posts
    4,696

    Default

    Quote Originally Posted by chinmay View Post
    Hi Nishank,

    You can use this in Modified Javascript Step.

    var sdf = new java.text.SimpleDateFormat("yyyy/MM/dd");
    var Date = sdf.format(YourDate);

    Thanks
    Chinmaya
    This is actually less efficient that using the Select Values step with the error handling.
    Use the Modified JavaScript Values step as a last resort.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.