Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiFiremonkeyIOSOSXWindows

Create A Custom Mask Edit Validation Event In Delphi XE7 Firemonkey On Android And IOS

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE7 Firemonkey Mask Edit | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSIf you are doing any kind of validation of fields with a TEdit component in Delphi XE7 Firemonkey you may be looking for a TMaskEdit control. Delphi VCL has a TMaskEdit but Firemonkey on Android, IOS, Windows, and Mac does not ship with one. This is easily solved however with some custom code in the OnKeyDown event. Basically the general idea is that when the user presses a key you check the key being pressed and the contents of the edit to see if it matches what you want it to accept. If the key should not be allowed you set it’s value to #0. I adapted my code snippet from a Delphi VCL example. One item of note is that I use String.IndexOf() in the code snippet which has cross platform support verses the Pos() function which gives you different values depending on if it is a mobile platform (0 based) or a desktop platform (1 based). This code snippet is really just to get you started with a mask edit solution. There are other things you will need to take into account as well like for example on Android the user can select entire auto complete words from the keyboard. You’ll have to come up with your own code for the OnChange event to handle those. TMS Pack for Firemonkey also has some edit components that support masking. The code snippet sample below shows how to filter for a decimal. It should also work in Appmethod.
procedure TForm1.MyEditKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
if not CharInSet(KeyChar,['.', '0'..'9']) then
KeyChar := #0;
if (KeyChar='.') AND (MyEdit.Text.IndexOf('.')>-1) then
KeyChar := #0;

Check out a Delphi VCL example of creating a custom masked edit for Delphi VCL and then adapt the code to Firemonkey yourself.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging


Unleashing Creativity With Song Writer AI: A Deep Dive


How To Build Stable Diffusion Text To Image Prompts


FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

1 Comment

Leave a Reply