Skip to content

Commit dd8da67

Browse files
authored
Move/positive decimal regex (#2071)
* Update and rename Allow positive and decimal values.js to Allow positive and decimal values.js Moved the file to the Regex folder. Refactored the code: - use clearValue instead of setValue ''. - fieldName to make it easier to adjust and use. - Include comment to describe the Regex * Update and rename readme.md to readme.md Moved the readme and made the text more descriptive. Included a description of what the validation actually does.
1 parent e5df68b commit dd8da67

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

Client-Side Components/Client Scripts/Allow positive and decimal values/Allow positive and decimal values.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

Client-Side Components/Client Scripts/Allow positive and decimal values/readme.md

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading || newValue == '') {
3+
return;
4+
}
5+
6+
// Regex: allows positive integers or decimals with up to two decimal places
7+
var regex = /^[0-9]+(\.\d{1,2})?$/;
8+
9+
// Regex explanation:
10+
// ^ : Start of string
11+
// [0-9]+ : One or more digits (whole number part)
12+
// (\.\d{1,2})? : Optional decimal part
13+
// \. : Literal decimal point
14+
// \d{1,2} : One or two digits after the decimal
15+
// $ : End of string
16+
// This pattern matches positive integers or decimals with up to two decimal places
17+
18+
// Replace 'amount' with the actual field name this script is attached to
19+
var fieldName = "amount";
20+
21+
if (!regex.test(newValue) || newValue <= 0) {
22+
g_form.clearValue(fieldName);
23+
g_form.showFieldMsg(fieldName, 'Please enter a value greater than zero. Decimals are allowed (up to two places).', 'error');
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Numeric Field Validation Script
2+
3+
This script validates numeric input in a form. It is used to ensure that values entered in fields like amount or price are properly formatted.
4+
5+
### Validation Rules
6+
7+
- Only positive numbers are allowed
8+
- Decimals are allowed, up to two digits
9+
- Zero or negative values are not accepted
10+
- Invalid input will clear the field and show an error message

0 commit comments

Comments
 (0)