B4A Class [B4X][XUI] xRegexField - regex validated data input


Licensed User
Hi all,

please find attached my porting to B4X/XUI of an old and very useful B4J class from @Erel.
Fundamentally it is an EditText (or TextField) with a few properties useful to check user input and react accordingly.

Pattern: used to hold a regex pattern in order to validate input data
SafeValue: a backup value to be returned if input data is invalid
ForceSafeVal: if True forces the class to return the SafeValue above
BorderColor: border color to signal the user his/her data doesn't match the pattern
BorderWidth: border width used with BorderColor above
BorderRadius: border radius used with BorderColor above
LetFocusOut: if True allows the focus to leave the control even if data is invalid
KbdType (B4A only): specifies one of three possible soft keyboards (TEXT, NUMBERS, DECIMAL_NUMBERS)

How one could use xRegexField?. Let's suppose we have to enter a monetary value, two decimals.
Just set
- Pattern = [0-9]+(\.[0-9][0-9])?
- SafeValue = 0.00
- ForceSafeVal = True/False (it depends on you; experiment and look at the returned value)
- BorderColor = #FFFD121C (a nice red color)
- BorderWidth = 2
- BordrRadius = 4
- LetFocusOut = True for standard behaviour, i.e. focus leaves the control even if it shows invalid data
- KbdType = DECIMAL_NUMBERS (so the soft keyboard shows the decimal point)

In your code make use of
xRegexField1.Value : reads/sets the current value
xRegexField1.IsValidInput: True if current value is a vaild one based on Pattern matching

Class calls FocusChanged and TextChanged in calling module, if set.

Note: this is a quick porting and a not so much tested version, so please don't use in production code.

Ver. 0.15 : initial version



Last edited: