diff --git a/Client-Side Components/Client Scripts/Field Validation/README.md b/Client-Side Components/Client Scripts/Field Validation/README.md deleted file mode 100644 index 9e5f32c701..0000000000 --- a/Client-Side Components/Client Scripts/Field Validation/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Field valiation for Special Characters on any table - -With this onChange Client script you can validate if there are any special characters present in the input given by user in a particular field and show an error message below the field and clear the field value. -Although we have various methods to do this, it is much easier and you can customize your error message. diff --git a/Client-Side Components/Client Scripts/Field Validation/specialcharacter.js b/Client-Side Components/Client Scripts/Field Validation/specialcharacter.js deleted file mode 100644 index 8488adbee2..0000000000 --- a/Client-Side Components/Client Scripts/Field Validation/specialcharacter.js +++ /dev/null @@ -1,15 +0,0 @@ -function onChange(control, oldValue, newValue, isLoading, isTemplate) { - - if (isLoading || newValue === '') { - return; - } - - //In the below regex, you can add or remove any special characters as per your requirement - var special_chars = /[~@|$^<>\*+=;?`')[\]]/; - - if (special_chars.test(newValue)) { - g_form.clearValue(''); - g_form.showErrorBox('','Special Characters are not allowed'); - } - -} diff --git a/Specialized Areas/Regular Expressions/Check for special characters/readme.md b/Specialized Areas/Regular Expressions/Check for special characters/readme.md new file mode 100644 index 0000000000..785c0366bc --- /dev/null +++ b/Specialized Areas/Regular Expressions/Check for special characters/readme.md @@ -0,0 +1,26 @@ +# Special Characters Validation (onChange Client Script) + +This script validates user input in a specific field and prevents the use of disallowed special characters. +It is designed to run as an **onChange client script** . + +## Functionality + +- When the user changes the value of a field, the script checks if the new value contains any special characters. +- If disallowed characters are found, the field is cleared and an error message is displayed to the user. +- The validation uses a regular expression that includes common special characters such as `~`, `@`, `|`, `$`, `^`, `<`, `>`, `*`, `+`, `=`, `;`, `?`, `` ` ``, `'`, `(`, `)`, `[`, and `]`. + +## How to Use + +1. Add the script as an **onChange client script** on the field you want to validate. +2. Replace the placeholder `''` in the script with the actual field name. +3. Customize the regular expression if you want to allow or block different characters. + +## Example Behavior + +- Input: `Hello@World` → ❌ Invalid → Field is cleared, error message shown. +- Input: `HelloWorld` → ✅ Valid → No action taken. + +## Notes + +- The script uses `g_form.clearValue()` to reset the field and `g_form.showErrorBox()` to display feedback. + diff --git a/Specialized Areas/Regular Expressions/Check for special characters/specialcharacter.js b/Specialized Areas/Regular Expressions/Check for special characters/specialcharacter.js new file mode 100644 index 0000000000..578247e66c --- /dev/null +++ b/Specialized Areas/Regular Expressions/Check for special characters/specialcharacter.js @@ -0,0 +1,24 @@ +function onChange(control, oldValue, newValue, isLoading, isTemplate) { + // Exit early if the form is loading, in template mode, or the new value is empty + if (isLoading || newValue === '') { + return; + } + + /** + * Define a regular expression to match disallowed special characters. + * Breakdown of the pattern: + * [~@|$^<>*+=;?`'\)\(\[\]] + * * - Square brackets [] define a character class, meaning "match any one of these characters". + * - ~ @ | $ ^ < > * + = ; ? ` ' ) ( [ ] : These are the characters being checked. + * - Characters that have special meaning inside a character class (like `(`, `)`, `[`, `]`) must be escaped with a backslash `\`. + */ + var disallowedChars = /[~@|$^<>*+=;?`'\)\(\[\]]/; + + var fieldName = ''; // Replace with the actual field name being validated + + // Check if the new value contains any disallowed characters + if (disallowedChars.test(newValue)) { + g_form.clearValue(fieldName); + g_form.showErrorBox(fieldName, 'Special characters are not allowed.'); + } +}