Real-time Field Validation
Validate field input in real-time and provide immediate feedback to users (e.g., check format, valid values, etc.).
onChange Table: incident Field: u_email
#validation #real-time #onChange #field-validation #email #regex
Script Code
JavaScript
1function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2 // Exit if form is loading
3 if (isLoading) {
4 return;
5 }
6
7 // Configuration: Define validation rules for different scenarios
8 var fieldName = 'u_email'; // Change to your field name
9 var value = newValue;
10
11 // Clear any previous validation messages
12 g_form.hideFieldMsg(fieldName, true);
13
14 // Skip validation if field is empty
15 if (!value || value === '') {
16 return;
17 }
18
19 // Example 1: Email validation
20 var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
21 if (!emailRegex.test(value)) {
22 g_form.showFieldMsg(fieldName, 'Please enter a valid email address', 'error');
23 return;
24 }
25
26 // Example 2: Phone number validation (US format)
27 // Uncomment and modify for phone validation
28 /*
29 var phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
30 if (!phoneRegex.test(value)) {
31 g_form.showFieldMsg('phone', 'Please enter a valid phone number (xxx-xxx-xxxx)', 'error');
32 return;
33 }
34 */
35
36 // Example 3: Minimum/Maximum length validation
37 /*
38 if (value.length < 5) {
39 g_form.showFieldMsg(fieldName, 'Must be at least 5 characters long', 'error');
40 return;
41 }
42 if (value.length > 100) {
43 g_form.showFieldMsg(fieldName, 'Cannot exceed 100 characters', 'error');
44 return;
45 }
46 */
47
48 // Example 4: Numeric range validation
49 /*
50 var numValue = parseFloat(value);
51 if (isNaN(numValue)) {
52 g_form.showFieldMsg(fieldName, 'Please enter a valid number', 'error');
53 return;
54 }
55 if (numValue < 0 || numValue > 100) {
56 g_form.showFieldMsg(fieldName, 'Value must be between 0 and 100', 'error');
57 return;
58 }
59 */
60
61 // Example 5: URL validation
62 /*
63 var urlRegex = /^(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/;
64 if (!urlRegex.test(value)) {
65 g_form.showFieldMsg(fieldName, 'Please enter a valid URL', 'error');
66 return;
67 }
68 */
69
70 // Show success message (optional)
71 g_form.showFieldMsg(fieldName, 'Valid email format', 'info');
72}
How to Use
1. Create an onChange Client Script on your table
2. Set the field to the one you want to validate
3. Customize the validation logic using the examples provided
4. Uncomment and modify examples as needed
5. Test with various valid and invalid inputs
Related Scripts
Auto-populate Related Fields
Automatically populate fields when a reference field changes (e.g., populate caller's phone and email when caller is selected).
Conditional Mandatory Fields
Make fields mandatory based on the value of another field (e.g., require close notes only when state is Resolved).
Dynamic Field Visibility
Show or hide fields based on another field's value (e.g., show "Other reason" field only when user selects "Other" from dropdown).