SanitizeTextField
Advanced text field sanitization utility with mathematical operation support.
SanitizeTextField provides comprehensive text field value sanitization for numeric inputs with support for mathematical operations, range constraints, and intelligent formatting. It handles both integer and floating-point values with configurable precision and validation.
Features:
- Integer and float value sanitization with min/max constraints
- Mathematical expression evaluation (+, -, *, /)
- Configurable rounding and precision control
- Real-time and completion-based formatting
- Automatic value clamping to specified ranges
- Empty value handling with default assignment
- Regex-based input cleaning and validation
Example usage:
// Sanitize integer input
SanitizeTextField.setTextValueToInt(field, "123", 0, 1000);
// Sanitize float with math operations
SanitizeTextField.setTextValueToFloat(field, "10+5", 0.0, 100.0, 10, true);
// Handle mathematical expressions
var hasOp = SanitizeTextField.applyFloatOrIntOperationsIfNeeded(
field, "25*2", 0.0, 1000.0, false, 1
);
Static Members
Size of the closure cache for performance optimization
setTextValueToInt(field: BaseTextFieldView, textValue: String, minValue: Int, maxValue: Int): Void
Sanitizes and sets an integer value to a text field with range validation.
Parses the text input as an integer, validates it within the specified range, and updates both the field's value and display text. Handles empty input gracefully by preserving the trimmed text.
Name | Type | Description |
---|---|---|
field |
BaseTextFieldView | The text field to update |
textValue |
String | The text input to parse |
minValue |
Int | Minimum allowed integer value |
maxValue |
Int | Maximum allowed integer value |
Sets a default integer value when the field is empty.
Assigns a default value of 0 (clamped to the valid range) when the text field is empty or needs a default value.
Name | Type | Description |
---|---|---|
field |
BaseTextFieldView | The text field to update |
minValue |
Int | Minimum allowed integer value |
maxValue |
Int | Maximum allowed integer value |
Returns | Description |
---|---|
Int | The assigned default value |
setTextValueToFloat(field: BaseTextFieldView, textValue: String, minValue: Float, maxValue: Float, round: Int, finishing: Bool): Void
Sanitizes and sets a float value to a text field with advanced formatting.
Parses the text input as a float with intelligent formatting that preserves decimal editing state. Supports comma-to-period conversion, range validation, and configurable rounding. The finishing parameter controls whether to apply final formatting or preserve the user's editing state.
Name | Type | Description |
---|---|---|
field |
BaseTextFieldView | The text field to update |
textValue |
String | The text input to parse |
minValue |
Float | Minimum allowed float value |
maxValue |
Float | Maximum allowed float value |
round |
Int | Rounding factor (1 = integer, >1 = decimal places) |
finishing |
Bool | Whether to apply final formatting (true) or preserve editing state (false) |
setEmptyToFloat(field: BaseTextFieldView, minValue: Float, maxValue: Float, round: Int): Float
Sets a default float value when the field is empty.
Assigns a default value of 0.0 (clamped to the valid range and rounded according to the specified precision) when the text field is empty.
Name | Type | Description |
---|---|---|
field |
BaseTextFieldView | The text field to update |
minValue |
Float | Minimum allowed float value |
maxValue |
Float | Maximum allowed float value |
round |
Int | Rounding factor (1 = integer, >1 = decimal places) |
Returns | Description |
---|---|
Float | The assigned default value |
Resets a text field to display '0' for empty float values.
Simple utility method to set the text field to display '0' and trigger a text value invalidation for UI updates.
Name | Type | Description |
---|---|---|
field |
BaseTextFieldView | The text field to reset |
applyFloatOrIntOperationsIfNeeded(field: BaseTextFieldView, textValue: String, minValue: Float, maxValue: Float, castToInt: Bool, round: Int): Bool
Evaluates and applies mathematical operations in text input.
Detects mathematical expressions in the text input (+, -, *, /) and evaluates them, then applies the result to the field. Supports both integer and float operations with proper range validation and rounding.
The method looks for operators in the text and attempts to parse the operands on either side. If the operation is valid, it computes the result and updates the field. If the operation is invalid but one operand is valid, it uses that operand as the value.
Name | Type | Description |
---|---|---|
field |
BaseTextFieldView | The text field to update |
textValue |
String | The text input potentially containing math operations |
minValue |
Float | Minimum allowed value |
maxValue |
Float | Maximum allowed value |
castToInt |
Bool | Whether to treat the result as an integer |
round |
Int | Rounding factor for float values |
Returns | Description |
---|---|
Bool | true if a mathematical operation was found and processed, false otherwise |
Private Members
Regular expression for matching numeric prefixes
Regular expression for matching non-digit and non-dot characters
Regular expression for matching spaces (from TextUtils)