- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 12:21 PM
I have a client asking to validate a proposed date field in catalog form that is either the 1st or 16th day of a future month. For example, in onSubmit script, if I submit a form today, the next acceptable proposed date will be April 1st or 16th or May, June, July, or later on. How can I validate if the user picks either the 1st or 16th day of the future month? I am not very good at script calculating on the date. Can anyone help me with this, or do you have a similar code sample?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 08:29 PM
Please try below code in onsubmit client script, just replace correct field names in code:
function onSubmit() {
// Get the proposed date from the form
var proposedDate = g_form.getValue('proposed_date_field'); // Replace 'proposed_date_field' with the actual field name of the proposed date
// Parse the proposed date to a JavaScript Date object
var proposedDateObj = new Date(proposedDate);
var currentDate = new Date();
// Check if the proposed date is in the future
if (proposedDateObj > currentDate) {
// Check if the proposed date is the 1st or 16th day of the month
var dayOfMonth = proposedDateObj.getDate();
if (dayOfMonth === 1 || dayOfMonth === 16) {
// Proceed with form submission
return true;
} else {
// Display error message and prevent form submission
alert('Please select either the 1st or 16th day of a future month.');
return false;
}
} else {
// Display error message and prevent form submission
alert('Please select a future date.');
return false;
}
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 12:56 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 01:22 PM
Hi @bbf35621 ,
Use onChange() Client Script with GlideAjax on your date restrict the value in date field.
Try this in the background script.
var gd = new GlideDate(); gs.info(gd.getDayOfMonthNoTZ()); if(gd.getDayOfMonthNoTZ() > 1) gs.print('Only 1st date of month is allowed.'); else gs.print('Date is correct.');
For Scoped Application, see the below image for reference.
For Global Scope Application, see the below image for reference.
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Thanks & Regards,
Sumanth Meda
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 03:37 PM
I tried your sample code and it didn't work. The gs.info printed, "The selected day is undefined". Here is my script include,
var dateId = this.getParameter('sysparm_date');
gs.info('The date is ' + dateId);
var gd = new GlideDateTime(dateId);
gs.info('The selected day is ' + gd.getDayOfMonthNoTZ());
if(gd.getDayOfMonthNoTZ() == 1 || gd.getDayOfMonthNoTZ() == 16){
gs.info('That is CORRECT!');
}else{
gs.info('User selected the wrong date');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 03:56 PM
Hi @bbf35621 ,
Can you try this:
var today = gs.localtime();
var today = gs.localtime();
var currentMonth = today.getMonth() + 1;
var currentYear = today.getFullYear();
var proposedDate = current_variable.proposed_date; // Replace 'current_variable' with your actual variable name
// Check if proposed date is in the future month
if (proposedDate.getMonth() !== currentMonth) {
// Check if proposed day is 1st or 16th
if (proposedDate.getDate() === 1 || proposedDate.getDate() === 16) {
// Valid date
return true;
} else {
// Invalid date - Not 1st or 16th
gs.addErrorMessage("Proposed date must be the 1st or 16th day of a future month.");
return false;
}
} else {
// Invalid date - Not a future month
gs.addErrorMessage("Proposed date must be in a future month.");
return false;
}
Johnny
Please mark this response as correct or helpful if it assisted you with your question.