September 16, 2021

Power Apps Validation Tip - End Date and time should be greater than Start Date and time

Overview:

We implemented several Power Apps forms for a consulting firm based out of Alpharetta, Georgia, United States. We came across a scenario to validate Start Date & End Date in one of the forms. In this article, we will talk about how we can apply DateTime validation in our Power Apps Form. Most of the time, we need to implement, one most common validation in Power Apps where End DateTime must be greater than Start DateTime. How can we apply that validation? Let’s see!

We have the following form in our Power Apps. We want to validate, check-out time should be greater than check-in time! So, how can we achieve this?


Step 1:

Please check the Datacard for Check-In and Check-Out time.

Step 2:

Add the following line of code on below highlighted controls! Need to add the same code on all following controls!

 If(  
   Or(  
     IsBlank(  
       DateValue2.SelectedDate + Time(  
         Value(HourValue2.Selected.Value),  
         Value(MinuteValue2.Selected.Value),  
         0  
       )  
     ),  
     IsBlank(  
       DateValue1.SelectedDate + Time(  
         Value(HourValue1.Selected.Value),  
         Value(MinuteValue1.Selected.Value),  
         0  
       )  
     )  
   ),  
   Set(  
     IsEndDateValid,  
     true  
   ),  
   DateDiff(  
     DateValue1.SelectedDate + Time(  
       Value(HourValue1.Selected.Value),  
       Value(MinuteValue1.Selected.Value),  
       0  
     ),  
     DateValue2.SelectedDate + Time(  
       Value(HourValue2.Selected.Value),  
       Value(MinuteValue2.Selected.Value),  
       0  
     ),  
     Minutes  
   ) > 0,  
   Set(  
     IsEndDateValid,  
     true  
   ),  
   Set(  
     IsEndDateValid,  
     false  
   )  
 )  

Step 3:

Set Validation message
 If(!IsEndDateValid, "Ënd Date Time should be greater than Start Date Time")  

Step 4:

Set Visible property for an Error message.

Step 5:

Prevent form for submitting an invalid entry by setting up the below condition for “OnSave”. If you are using a standalone Canvas app, then you need to write the same code on your button click!

Step 6:

Change "OnNew" Property of form. If you are using a standalone Canvas app, then you need to write the same code on the New button of the form!
 NewForm(SharePointForm1);Set(IsEndDateValid,true)  

Step 7:

Save and Publish form!

Conclusion:

This is how we can simply apply end DateTime validation for Power Apps! Happy Power Apping!

If you have any questions you can reach out our SharePoint Consulting team here.

3 comments:

  1. Great Post! One change I noticed in the 2 years since. I put this:
    IsEndDateValid,
    true
    ),
    in the OnStart Property, instead of the OnNew property.

    ReplyDelete
    Replies
    1. Thank you!

      In the case of a Canvas App in PowerApps, the code is typically placed in the "OnStart" event. However, for the scenario discussed in this blog, we are working with a Customized SharePoint List Form PowerApps. In this context, it's essential to place the code in the "OnNew" property of a SharePoint Form.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete