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

Formula which checks input for a character and replaces it

Hi,

 

So clients can pick a date (with the datepicker) or fill it in manually (which is faster, most of the time).

However, when they choose to fill it in manually, some clients use for example: 01/01/2019.

Because of the "/", it doesn't register. It has to be: 01-01-2019.

 

Is there a formula where it checks if the input has "/" and replace them with "-"?

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
MartinWoe
Level: Powered On

Re: Formula which checks input for a character and replaces it

Hi, I have a "dirty" workaround for you which I just tested successfully.

 

This workaround means, that you overlay the datepicker with a textinput field (see DatePickerOverlay.png).

 

If you are fine with it, here are the steps after creating textinput field and laying it over datepicker control.

 

initialize variable in screen's OnVisible command

UpdateContext({PickerDate: Today()})

Set Datepicker default to the variable PickerDate

Set Textinput field to variable PickerDate

 

Set DatePicker "On Change" property to

UpdateContext({PickerDate: (DatePicker1.SelectedDate)})

(just in case someone uses the datepicker button)

 

Set Textinput field "On Change" property to

UpdateContext({PickerDate: DateValue(Substitute(TextInput1.Text, "-", "/"))})

I also added a video as a link: DatePickerOverlay

12 REPLIES 12
Highlighted
Super User
Super User

Re: Formula which checks input for a character and replaces it

Two potential solutions.

  1. Set the Editable property of the datepicker to false.  That will force them to go through the picker on the calendar and they won't be able to manually input textual dates.
  2. If you still want the manual entry then try this in the OnChange property.
    Substitute( Text(DatePicker1.SelectedDate),"/","-")


-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Super User
Super User

Re: Formula which checks input for a character and replaces it

Hi @Saego 

 

To replace "/" with "-" in the DatePicker selected value, try out the below expression:
Substitute(Text(DatePicker1.SelectedDate),"/","-")
MicrosoftTeams-image (74).png

Hope this Helps!

 

Saego
Level: Powered On

Re: Formula which checks input for a character and replaces it

Hi there, thanks for the fast reply.

Sadly, the formula: Substitute( Text(DateValue1.SelectedDate);"/";"-") doesn't seem to work for me. 

Date test.png

When I write is as below, it still doesn't save even though I filled in the formula mentioned above on OnChange.

 

KarlHoffmaster
Level: Powered On

Re: Formula which checks input for a character and replaces it

If the value entered manually in to a “DatePicker” control is not a valid date format, then the resulting value is BLANK when referencing it as “DatePicker.SelectedDate”.  As a result, you cannot “Substitute” characters since the formula is seeing a blank result versus what the user entered manually.

One solution would be to force the user to enter the correct format.

Create a label where the text value is:  DatePicker1.SelectedDate.  Title it “Label2”.

If the value entered manually in the DatePicker1 field is not a valid date format (m/d/yyyy), then the label will be blank.

You can then conditionally format a text box to appear below the date picker that states “Please enter a valid date value format of (m/d/yyyy). “  You can even disable the ability to move forward with the process unless it is entered correctly.  For example, disable the other controls. 

The visibility formula for the text warning field would be:  If(Len(Text(DatePicker1.SelectedDate)) > 1 & Len(Label2) < 1, true, false)

The other solution would be to use a Text Input field and force the result to be a date value versus using a date picker. 

Additionally, you can dynamically set your default date or “Start Year” to something close to what the user would be entering so they would only have to highlight and change the Day or (month/day) versus entering an entirely new date.  

Confluent
Super User
Super User

Re: Formula which checks input for a character and replaces it

Hi @KarlHoffmaster 

 

The substitute function, does't work on the data picker itself, you can use this when you are fetching selected value in a control or Patching it to a data source. This is something which will portray what you have selected in data picker in the format specified(Replacing / with -). 
 
You can use this function to send the value in required format irrespective of what user has entered. SInce, powerapps does not restrict any format for user added values. Either you can turn off editing or leave it as it, but substitute the value while querying the data source.
 
Hope this Helps!
MartinWoe
Level: Powered On

Re: Formula which checks input for a character and replaces it

Hi, I have a "dirty" workaround for you which I just tested successfully.

 

This workaround means, that you overlay the datepicker with a textinput field (see DatePickerOverlay.png).

 

If you are fine with it, here are the steps after creating textinput field and laying it over datepicker control.

 

initialize variable in screen's OnVisible command

UpdateContext({PickerDate: Today()})

Set Datepicker default to the variable PickerDate

Set Textinput field to variable PickerDate

 

Set DatePicker "On Change" property to

UpdateContext({PickerDate: (DatePicker1.SelectedDate)})

(just in case someone uses the datepicker button)

 

Set Textinput field "On Change" property to

UpdateContext({PickerDate: DateValue(Substitute(TextInput1.Text, "-", "/"))})

I also added a video as a link: DatePickerOverlay

Saego
Level: Powered On

Re: Formula which checks input for a character and replaces it

Hi everyone,

 

Thanks for your thoughts. I used the workaround that @MartinWoe described. It works perfectly and it's exactly what I wanted:

  1. People still can use the datepicker if they desire.
  2. People can fill in the date manually in the 'datepicker' in both ways (d/m/y or d-m-y) because of the substitute.

 

Thanks!

Saego
Level: Powered On

Re: Formula which checks input for a character and replaces it

So I've encountered a new problem, using @MartinWoe workaround.

Whenever I fill in the date, save it, and then edit, the default is set to todays date. That has probably something to do with:

UpdateContext({PickerDate: Today()})

But I'm not sure how to fix this. I want to display the date I filled in earlier. 

 

Thanks in advance.

Super User
Super User

Re: Formula which checks input for a character and replaces it

@Saego 

 

If you dont want to set the default date as Today on page visible, you can just go ahead and remove the expression from OnVisible property of your screen and this won't set the variable.
 
If you are using an edit form for doing so, you can apply an if loop to check if the form mode is Edit/New. Based on the condition, you can set the variable.
 
Hope this Helps!

If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up. THANKS!