How to validate date which is type of glide_date?

pavankumary
Kilo Contributor

Hi All,

Could you please help with validating date with type (glide_date) in ServiceNow? this field only contains date value and not the date and time format?

Script should validate the date automatically with respect to system/user date format.

Kindly help ASAP

thanks,

Pavan

4 REPLIES 4

Rajesh Mushke
Mega Sage
Mega Sage

Hi Pavan,




To validate the input of all date/time fields, you can use the following in a validation script (System Definition > Validation Scripts). Because the date/time format is hard coded in this script, it must match your instance's date/time format. If your instance's date/time format changes, you must update your validation script.


Set the validation script's type to Date/Time. Then, with this validation script, if a user enters an incorrect format in a date/time field, they will receive an error message.



function validate(value) {


if(!value)


return true;   // empty fields are still valid dates



// We "should" have the global date format defined always defined. but there's always that edge case...


if(typeof g_user_date_time_format !== 'undefined')


return isDate(value, g_user_date_time_format);



// if we don't have that defined, we can always try guessing


return parseDate(value) !== null;


}


DateTimeValidation1.png


Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

Hi Rajesh,



Thanks for your reply



This will work for the fields of type date and time, but in case of type glide_date (which contains only date not time) it is not working



Could you please suggest if any other way?



Thanks,


Pavan


Hello,

In case someone comes to this old thread, you can (now) use g_user_date_format if you are checking a date without time in Client Script :

 

 

return isDate(value, g_user_date_format) ;

 

 

Best regards

Riya Verma
Kilo Sage
Kilo Sage

Hi @pavankumary ,

 

Hope you are doing great.

 

To validate a date field with the "glide_date" type in ServiceNow and ensure it adheres to the system/user date format, you can follow these steps:

  1.  Create a Business Rule in ServiceNow that triggers when a record is inserted or updated. Step 2: In the Business Rule, use a script to validate the "glide_date" field.
  2. In the Business Rule, use a script to validate the "glide_date" field.

 

// Business Rule Name: Validate Glide Date Field
// Table: <Name of the table where the glide_date field is located>
// When: Before

(function executeRule(current, previous /*, g*/ ) {
    // Get the user's preferred date format from the system properties
    var userDateFormat = gs.getProperty('glide.sys.date_format');

    // Get the date value from the glide_date field
    var inputDate = current.glide_date_field; // Replace 'glide_date_field' with the actual field name

    // Validate the date format
    if (!isValidDateFormat(inputDate, userDateFormat)) {
        // If the date format is invalid, set an error message on the glide_date field
        current.glide_date_field.setError('Invalid date format. Please use the format: ' + userDateFormat);
    }
})(current, previous);

// Function to validate date format
function isValidDateFormat(dateString, format) {
    // Use ServiceNow's date parsing function to check if the date is in the correct format
    var parsedDate = gs.dateGenerate(dateString, format);
    return parsedDate.isValid();
}

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma