Skip to main content

Notifications

Power Apps - Building Power Apps
Suggested answer

a

(1) ShareShare
ReportReport
Posted on by 691
 
  • WarrenBelz Profile Picture
    WarrenBelz 145,343 on at
    Automatically format an entry in a TextInput field
    A quick follow-up to see if you received the answer you were looking for or if you need further assistance.

    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee
  • WarrenBelz Profile Picture
    WarrenBelz 145,343 on at
    Automatically format an entry in a TextInput field
    OK - I do not think it is possible (without a lot of brain frying) to cover every possibility of what a user might enter. Also if you want to change numbers to xx/xx/xxxx "on the fly", you will need to set the Default of your text box to (in the example) varDate, so you may need to clear this before the user enters the next date, My best effort below
    If(
       Len(Self.Text) > 0;
       If(
          !("/" in Self.Text) && Len(Self.Text) = 8 && IsNumeric(Self.Text);
          UpdateContext({varDate: Left(Self.Text; 2) & "/" & Mid(Self.Text; 3; 2) & "/" & Right(Self.Text; 4)});;
          Reset(Self);
          UpdateContext({varDate: Self.Text})
       );;
       With(
          {dateParts: Split(varDate; "/")};
          With(
             {
                day: Value(First(dateParts).Value);
                month: Value(Index(dateParts; 2).Value);
                year: Value(Last(dateParts).Value);
                enteredDate: DateValue(varDate; "fr-FR");
                minValidDate: DateAdd(Today(); 3; TimeUnit.Months);
                maxValidDate: DateAdd(Today(); 3; TimeUnit.Years)
             };
             With(
                {
                   _ErrorDates: 
                   month = 1 && day > 31 || 
                   month = 2 && day > If(Mod(year; 4) = 0; 29; 28) || 
                   month = 3 && day > 31 || 
                   month = 4 && day > 30 || 
                   month = 5 && day > 31 || 
                   month = 6 && day > 30 || 
                   month = 7 && day > 31 || 
                   month = 8 && day > 31 || 
                   month = 9 && day > 30 || 
                   month = 10 && day > 31 || 
                   month = 11 && day > 30 || 
                   month = 12 && day > 31;
                   _ErrorFormat: !IsMatch(varDate; "^\d{2}/\d{2}/\d{4}$");
                   _ErrorMax: enteredDate > maxValidDate;
                   _ErrorMin: enteredDate < minValidDate
                };
                With(
                   {_Error: _ErrorDates || _ErrorFormat || _ErrorMax || _ErrorMin};
                   If(
                      _Error;
                      Notify(
                         If(
                            _ErrorDates;
                            "Date invalide.";
                            _ErrorFormat;
                            "Date invalide. Veuillez entrer une date au format JJ/MM/AAAA.";
                            _ErrorMax;
                            "La date doit être inférieure à 3 ans.";
                            _ErrorMin;
                            "La date doit être supérieure à 3 mois."
                         );
                         NotificationType.Error
                      );;
                      UpdateContext({varDate: ""});;
                      Reset(Self)
                   )
                )
             )
          )
       )
    )
     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee
  • s44 Profile Picture
    s44 691 on at
    Automatically format an entry in a TextInput field
    I used the formula you indicated. But when I enter a date in the form DDMMYYYY in my TextInput, the entry is not formatted in DD/MM/YYYY when I leave the field.
     
  • WarrenBelz Profile Picture
    WarrenBelz 145,343 on at
    Automatically format an entry in a TextInput field
    Hi @s44,
    Please see addition (which is good to check anyway)
     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee
  • s44 Profile Picture
    s44 691 on at
    Automatically format an entry in a TextInput field
     I get the error : Cannot use index on empty table  :( 
  • Suggested answer
    WarrenBelz Profile Picture
    WarrenBelz 145,343 on at
    Automatically format an entry in a TextInput field
    Hi @s44,
    A bit different approach - your various message requirements made this more complex
    If(
      "/" in Self.Text;
    With( {dateParts: Split(Self.Text; "/")}; With( { day: Value(First(dateParts).Value); month: Value(Index(dateParts; 2).Value); year: Value(Last(dateParts).Value); enteredDate: DateValue(Self.Text; "fr-FR"); minValidDate: DateAdd(Today(); 3; TimeUnit.Months); maxValidDate: DateAdd(Today(); 3; TimeUnit.Years) }; With( { _ErrorDates: month = 1 && day > 31 || month = 2 && day > If(Mod(year; 4) = 0; 29; 28) || month = 3 && day > 31 || month = 4 && day > 30 || month = 5 && day > 31 || month = 6 && day > 30 || month = 7 && day > 31 || month = 8 && day > 31 || month = 9 && day > 30 || month = 10 && day > 31 || month = 11 && day > 30 || month = 12 && day > 31; _ErrorFormat: !IsMatch(Self.Text; "^\d{2}/\d{2}/\d{4}$"); _ErrorMax: enteredDate > maxValidDate; _ErrorMin: enteredDate < minValidDate }; With( {_Error: _ErrorDates || _ErrorFormat || _ErrorMax || _ErrorMin}; If( _Error; Notify( If( _ErrorDates; "Date invalide."; _ErrorFormat; "Date invalide. Veuillez entrer une date au format JJ/MM/AAAA."; _ErrorMax; "La date doit être inférieure à 3 ans."; _ErrorMin; "La date doit être supérieure à 3 mois." ); NotificationType.Error );; Reset(Self) ) ) ) )
      ) )

     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,343

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,703

Leaderboard