Skip to content

Commit 3d12a1f

Browse files
authored
Regex/move and improve (#2006)
* Create folder and readme.md - Created sub-folder "Check for special characters" in the Regular Expressions folder. - Added readme * Update and rename specialcharacter.js to specialcharacter.js - Moved the script to the new created folder in Regular Expressions. - Refactored/fixed the code: -- Renamed regex variable to disallowedChars for clarity. -- Escaped special characters inside the regex to correctly match (, ), [, and ]. -- Added detailed comments explaining the purpose and structure of the regular expression. -- Replaced placeholder field name with a variable for easier reuse and readability. -- Improved error messaging to clearly inform users about invalid input. * Delete Client-Side Components/Client Scripts/Field Validation directory Deleted the old folder. Files in the folder have moved to the Regex folder. * Update readme.md Expanded and clarified the Readme. Also included in the description the updated script.
1 parent 07adccc commit 3d12a1f

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

Client-Side Components/Client Scripts/Field Validation/README.md

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

Client-Side Components/Client Scripts/Field Validation/specialcharacter.js

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Special Characters Validation (onChange Client Script)
2+
3+
This script validates user input in a specific field and prevents the use of disallowed special characters.
4+
It is designed to run as an **onChange client script** .
5+
6+
## Functionality
7+
8+
- When the user changes the value of a field, the script checks if the new value contains any special characters.
9+
- If disallowed characters are found, the field is cleared and an error message is displayed to the user.
10+
- The validation uses a regular expression that includes common special characters such as `~`, `@`, `|`, `$`, `^`, `<`, `>`, `*`, `+`, `=`, `;`, `?`, `` ` ``, `'`, `(`, `)`, `[`, and `]`.
11+
12+
## How to Use
13+
14+
1. Add the script as an **onChange client script** on the field you want to validate.
15+
2. Replace the placeholder `'<your_field_name>'` in the script with the actual field name.
16+
3. Customize the regular expression if you want to allow or block different characters.
17+
18+
## Example Behavior
19+
20+
- Input: `Hello@World` → ❌ Invalid → Field is cleared, error message shown.
21+
- Input: `HelloWorld` → ✅ Valid → No action taken.
22+
23+
## Notes
24+
25+
- The script uses `g_form.clearValue()` to reset the field and `g_form.showErrorBox()` to display feedback.
26+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2+
// Exit early if the form is loading, in template mode, or the new value is empty
3+
if (isLoading || newValue === '') {
4+
return;
5+
}
6+
7+
/**
8+
* Define a regular expression to match disallowed special characters.
9+
* Breakdown of the pattern:
10+
* [~@|$^<>*+=;?`'\)\(\[\]]
11+
* * - Square brackets [] define a character class, meaning "match any one of these characters".
12+
* - ~ @ | $ ^ < > * + = ; ? ` ' ) ( [ ] : These are the characters being checked.
13+
* - Characters that have special meaning inside a character class (like `(`, `)`, `[`, `]`) must be escaped with a backslash `\`.
14+
*/
15+
var disallowedChars = /[~@|$^<>*+=;?`'\)\(\[\]]/;
16+
17+
var fieldName = '<your_field_name>'; // Replace with the actual field name being validated
18+
19+
// Check if the new value contains any disallowed characters
20+
if (disallowedChars.test(newValue)) {
21+
g_form.clearValue(fieldName);
22+
g_form.showErrorBox(fieldName, 'Special characters are not allowed.');
23+
}
24+
}

0 commit comments

Comments
 (0)