cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
iDrive
Level: Powered On

Validate date when user only enters 2 digit year

I have a SharePoint PowerApps form with a date field that is not required. Even though the hint text is "12/31/2001" I've had users being users and only enter 2 digit years (e.g. 12/31/01). When they do this and hit save the date field is cleared out and the form saves without a date. Few questions:

  • Can the 2 digit years be recognized and converted to 4 digit years that are needed for users that aren't following directions? This probably isn't the best route but want to know my options.
  • Can validation be added to catch where only a 2 digits year has been entered and stop the form from saving and clearing out those fields along with providing the user an error message?

Date-empty.png
Date-2_digit_year.png

7 REPLIES 7
iDrive
Level: Powered On

Re: Validate date when user only enters 2 digit year

I've attempted to set a variable on change for the date field so I can validate off that variable but for some reason the onChange event only fires if a valid date (e.g. dd/mm/yyyy) is entered and you leave focus of the field and not when an invalid date (e.g. dd/mm/yy) is entered and leave focus of the field.

UpdateContext({
varStartDate:Value(DCValueStartDate01.SelectedDate)
})

 The variable value will remain empty until a valid date entered in the dd/mm/yyyy format is entered and you leave focus of the date field. Any idea as to how I can trigger the variable value to match the field value even if it's not a valid date?

Super User
Super User

Re: Validate date when user only enters 2 digit year

*Updated for Date Field formula

You have a few options to guide the user to entering the correct values, I would suggest using these two:

1. Have a label below the field with
Text=
If( IsMatch( Text( DCValueStartDate01.SelectedDate, "[$-en-US]dd/mm/yyyy"), "^\d{2}\/\d{2}\/\d{4}"), "", "Date format must be dd/mm/yyyy")

This will display nothing if user enters correct value, but tell them the correct format if it is incorrect.

 

2. Set your save/submit button to have a similar form of logic,

DisplayMode=

 If( IsMatch( Text( DCValueStartDate01.SelectedDate, "[$-en-US]dd/mm/yyyy"), "^\d{2}\/\d{2}\/\d{4}"), DisplayMode.Edit, DisplayMode.Disabled)

In this way they won't be able to click the submit button until they've fixed the error in their entry




Don't forget to 'Mark as Solution' if a post answered your question and always 'Thumbs Up' the posts you like or helped you!
iDrive
Level: Powered On

Re: Validate date when user only enters 2 digit year

Appreciate the response @iAm_ManCat


1. Have a label below the field with
Text=
If( IsMatch( Text( DCValueStartDate01.SelectedDate, "[$-en-US]dd/mm/yyyy"), "^\d{2}\/\d{2}\/\d{4}"), "", "Date format must be dd/mm/yyyy")

This will display nothing if user enters correct value, but tell them the correct format if it is incorrect. 

 


#1. While this method does work, it shows the "error message" the entire time until a date is entered in the correct format. With the date field not being required and sometimes not holding a value this is less than desirable. Showing the error message if what was entered is invalid only after the user leaves focus (i.e. onChange) is much cleaner and more desireable, any ideas on how to do this? Is there a known bug with the onChange event on a date field? I don't understand why it only fires when a valid date is entered and the user leaves the field.

 


2. Set your save/submit button to have a similar form of logic,

DisplayMode=

 If( IsMatch( Text( DCValueStartDate01.SelectedDate, "[$-en-US]dd/mm/yyyy"), "^\d{2}\/\d{2}\/\d{4}"), DisplayMode.Edit, DisplayMode.Disabled)

In this way they won't be able to click the submit button until they've fixed the error in their entry

#2. How do you add the logic to the save/submit buttons? As a reminder these are custom SharePoint forms and not a full fledged app.

Super User
Super User

Re: Validate date when user only enters 2 digit year

Hi,

 

Thanks for getting back to me, I'm not quite sure why the onChange doesn't trigger until valid date, but I would suspect that it only considers a valid date a change as it's a date-only field.

 

Regarding point 1. - we can apply an isBlank on the front to only show the error once the user enter's something:

If( IsBlank(DCValueStartDate01.SelectedDate), "", IsMatch( Text( DCValueStartDate01.SelectedDate, "[$-en-US]dd/mm/yyyy"), "^\d{2}\/\d{2}\/\d{4}"), "", "Date format must be dd/mm/yyyy")

So now it checks if its blank, then displays nothing, then if it matches the regex string, display nothing, otherwise display the error.

 

In terms of 2. - Yes apologies I had thought you used custom save buttons within the form, but if it's integrated SharePoint form there's no way to affect the save/customize bar along the top.

I do have a workaround I use at work for this, that is to have a field, hidden, and required, which is only filled when the above formula is satisfied.

So something like:

Default=
If( IsBlank(DCValueStartDate01.SelectedDate), "", IsMatch( Text( DCValueStartDate01.SelectedDate, "[$-en-US]dd/mm/yyyy"), "^\d{2}\/\d{2}\/\d{4}"), "Valid", "")

So if there's nothing entered, have nothing, if it matches then change text to Valid which will allow the form to submit, if it doesn't match the IsMatch then have it blank to stop it from submitting - at this point the error message will be visible too so it should be obvious to the user what they need to correct.

 

Do some testing with that and let me know how you get on, I can't think of any other ways to do it at the moment and this sort of setup has worked for me before,

 

Hope this helps!

 

Cheers,

 

@iAm_ManCat




Don't forget to 'Mark as Solution' if a post answered your question and always 'Thumbs Up' the posts you like or helped you!
iDrive
Level: Powered On

Re: Validate date when user only enters 2 digit year

Thanks for getting back to me, I'm not quite sure why the onChange doesn't trigger until valid date, but I would suspect that it only considers a valid date a change as it's a date-only field.

 


Hmm, this feels like a bug to me since the field focus is changing regardless of value. I'll make a sepearte post to bring this to their attention and get some clarifiaction.


Regarding point 1. - we can apply an isBlank on the front to only show the error once the user enter's something:

If( IsBlank(DCValueStartDate01.SelectedDate), "", IsMatch( Text( DCValueStartDate01.SelectedDate, "[$-en-US]dd/mm/yyyy"), "^\d{2}\/\d{2}\/\d{4}"), "", "Date format must be dd/mm/yyyy")

So now it checks if its blank, then displays nothing, then if it matches the regex string, display nothing, otherwise display the error.

 


Hmm, tried this out but now the error message just stays hidden the entire time regardless if the value is valid or not, can't figure out what is missing in it. Even tried the following wraping the value in the Text() formula but still nothing.

If( IsBlank(Text(DCValueStartDate01.SelectedDate)), "", IsMatch(Text(DCValueStartDate01.SelectedDate, "[$-en-US]dd/mm/yyyy"), "^\d{2}\/\d{2}\/\d{4}" ), "", "Date format must be dd/mm/yyyy")
Super User
Super User

Re: Validate date when user only enters 2 digit year

Hi!

 

Been playing around this morning and found a better solution - in the properties, there's an option called "isEditable", set this to false and users wont be able to enter their own dates with the keyboard, they have to select it using the Datepicker itself.

 

No need for validation this way 🙂

 

Cheers,

@iAm_ManCat




Don't forget to 'Mark as Solution' if a post answered your question and always 'Thumbs Up' the posts you like or helped you!
iDrive
Level: Powered On

Re: Validate date when user only enters 2 digit year

I saw this as an option when I was messing around on Friday but it's not the desired user experience my client was looking for.

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors
Users Online
Currently online: 463 members 6,503 guests
Please welcome our newest community members: