97
Views
9
Comments
Restrict user from entering future date.
Question
Application Type
Reactive

I am allowing user to pick date using date picker but, I want to restrict the user from entering future dates, the user should only be able to pick current date or past dates. How, do I implement this?

date picker properties.PNG
UserImage.jpg
Tarun Singh

I am not using calendar widget, I am just using simple input and set datatype as date and time. There i do not have any property to set Max and Min date.
I tried to add condition in the onSubmit action that if the input date is greater than current date then display error message, but it is not working.

2024-10-16 11-59-48
Nick Vandebriel

Hi Tarun,

You can use the datepicker widget and under optionalconfigs you can set a min and max date. For the max date you could just set currdate() 

UserImage.jpg
Tarun Singh

I am not allowed to use that widget I have to use simple input field.

2024-10-19 11-15-19
Sazid

@Tarun Singh -

You can achieve this using Javascript. Find attached oml where i have restricted selection to current date in input widget

Date_Demo.oml
2019-01-31 08-54-57
André Costa
Champion

If you are using a simple input field instead of the OutSystems Date Picker widget, you can still restrict the user from entering future dates by l setting the max attribute to the current date.

Here's how you can do it in OutSystems using a simple input field:

  1. Use an Input Field with type="date"

  2. Set the max Attribute: You can dynamically set the max attribute of the input field to today's date using OutSystems expressions. The max attribute ensures that users cannot select any date later than today.

    In the input field's Extended Properties, add the following property:

    • Name: max
    • Value: TextToDate(CurrDate())
    • This sets the max attribute to the current date, preventing users from selecting any future dates.

  3. Optional: Server-Side Validation: Even though the max attribute will restrict users from selecting future dates on the client side, it’s good practice to add server-side validation to ensure the selected date is not in the future.

    In your Save action or wherever you are processing the date input, add a condition:

    If( SelectedDate > CurrDate(),  Raise Validation Error)

2024-07-31 11-32-34
ndy

Dear @Tarun Singh 

If you’re working with a simple input field for date entry and need to prevent future dates, you can restrict this using a combination of input constraints and validation logic.

Client-Side

  • HTML Date Input Type with Max Attribute

<Input Type="Date" Max=Text(CurrDate(), "yyyy-MM-dd") />

You can set the input type to date and use the max attribute to restrict the input field to today’s date. Here’s how:

Binding Max to the current date formatted as "yyyy-MM-dd" to ensure the restriction works correctly. The Max attribute will make sure users can’t select any date beyond today’s date.

  • JavaScritp Validation

You can add a bit of JavaScript to handle the validation directly in the browser before submission.

Server-Side

Implement these in action flow:

  • On Form Submission: Add a conditional check that compares the input date with today’s date.
  • Display Error Message: If the date is greater than today’s date, display an error message.
2021-01-28 10-02-59
Muhammad Mahmudul Hasan

Hi @ndy 

Please don't post ChatGPT answer. Those answer is generic & will be no help here.

2020-07-21 19-28-50
Rajat Agrawal
Champion

Hi,

Apply below java script in onready event of screen

document.getElementById($parameters.Inputid).setAttribute("max", new Date().toISOString().split("T")[0]); 


after that apply the below java script on on input event of input field

const dateInput = document.getElementById($parameters.InputId);

const today = new Date().toISOString().split("T")[0];

dateInput.addEventListener("input", function() {

    if (dateInput.value > today) {

        dateInput.value = ""; 

        alert("Future dates are not allowed."); 

    }

});


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.