Sunday, March 31, 2019

Time Input Fields in PowerApps

When programming anything with time in Microsoft PowerApps, there seems to be no easy way to create a time field, that is able to input and validate time.

I have crated a script, that validates and formats a standard input field as a time field. This is working with 24h time, but it should give you an idea so you can expand it to whatever you want.

Create a text field, called "TimeInput".
Add the variable tTxtOut as "default" and the script below to "onChange". Set "HintText" to "hh:mm"

You can use the global variables in other parts of your code:
err: boolean - set to true if something is wrong
tTimeError: string - contains an error text  ("Invalid time!")
tTxtOut: string - contains the formattet time. Set the


// Set global variables

Set(inp, TimeInput.Text);
Set(th, "00");
Set(tm, "00");
Set(err, false);
Set(tTimeError, "");

//Validate length
If (Len(inp)>5, Set(err, true));

// Easy input without ":"
If (Len(inp)=3, Set(inp, Left(inp,2)&":"&Right(inp,1)&"0")); 
If (Len(inp)=4, Set(inp, Left(inp,2)&":"&Right(inp,2)));

// Validate hours
// Check for invalid characters
Set(th, First(Split(inp,":")).Result);
Set(h, Value(th));
If (th<>Text(h) And th<>"0"&Text(h), Set(err, true));
// Check limits
Set(th, Text(h));
If(h<0 Or h>23,Set(err, true));
If(h<10, Set(th, "0"&h));

// Validate minutes
// Check for invalid characters
Set(tm, Last(Split(inp,":")).Result);
Set(m, Value(tm));
If (tm<>Text(m) And tm<>"0"&Text(m), Set(err, true));
If(Find(":", inp)>0,m=m,Set(m, 0));
// Check limits
Set(tm, Text(m));
If(m<0 Or m>59, Set(err, true));
If(m<10, Set(tm, "0"&m));

// Do something
If(err, Set(tTimeError, "Invalide time!");Set(tTxtOut,inp), Set(tTxtOut, th&":"&tm))

Reviving my web site

 It's been years since I updated or even used this blog, but now I going to revive it. First of all - I have updated the design (to anot...