1(function (global) { 2 'use strict'; 3 4 var RULES = { 5 name: { min: 2, max: 70 }, 6 subject: { min: 10, max: 100 }, 7 message: { min: 20, max: 1500 } 8 }; 9 10 var FIELD_LABELS = { 11 name: 'Name', 12 subject: 'Subject', 13 message: 'Message' 14 }; 15 16 var EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; 17 18 function toText(value) { 19 if (value === null || value === undefined) { 20 return ''; 21 } 22 return String(value).trim(); 23 } 24 25 function inRange(value, min, max) { 26 var len = toText(value).length; 27 return len >= min && len <= max; 28 } 29 30 function validateName(value) { 31 var text = toText(value); 32 if (text.length === 0) { 33 return { type: 'empty', message: '' }; 34 } 35 if (text.length < RULES.name.min) { 36 return { type: 'min', message: 'Name must be at least ' + RULES.name.min + ' characters.' }; 37 } 38 if (text.length > RULES.name.max) { 39 return { type: 'max', message: 'Name must be at most ' + RULES.name.max + ' characters.' }; 40 } 41 return null; 42 } 43 44 function validateEmail(value) { 45 var text = toText(value); 46 if (text.length === 0) { 47 return { type: 'empty', message: '' }; 48 } 49 if (!EMAIL_REGEX.test(text)) { 50 return { type: 'invalid', message: 'Please enter a valid email address.' }; 51 } 52 return null; 53 } 54 55 function validateSubject(value, required) { 56 var text = toText(value); 57 if (text.length === 0) { 58 if (!required) return null; 59 return { type: 'empty', message: '' }; 60 } 61 if (text.length < RULES.subject.min) { 62 return { type: 'min', message: 'Subject must be at least ' + RULES.subject.min + ' characters.' }; 63 } 64 if (text.length > RULES.subject.max) { 65 return { type: 'max', message: 'Subject must be at most ' + RULES.subject.max + ' characters.' }; 66 } 67 return null; 68 } 69 70 function validateMessage(value) { 71 var text = toText(value); 72 if (text.length === 0) { 73 return { type: 'empty', message: '' }; 74 } 75 if (text.length < RULES.message.min) { 76 return { type: 'min', message: 'Message must be at least ' + RULES.message.min + ' characters.' }; 77 } 78 if (text.length > RULES.message.max) { 79 return { type: 'max', message: 'Message must be at most ' + RULES.message.max + ' characters.' }; 80 } 81 return null; 82 } 83 84 function getLimitMessage(fieldName, value) { 85 var fieldRule = RULES[fieldName]; 86 var fieldLabel = FIELD_LABELS[fieldName]; 87 var text = toText(value); 88 89 if (!fieldRule || !fieldRule.max || !fieldLabel) { 90 return ''; 91 } 92 93 if (text.length >= fieldRule.max) { 94 return fieldLabel + ' character limit reached (' + fieldRule.max + ').'; 95 } 96 97 return ''; 98 } 99 100 function validateForm(values, options) { 101 var opts = options || {}; 102 var subjectRequired = !!opts.subjectRequired; 103 104 var errors = { 105 name: validateName(values && values.name), 106 email: validateEmail(values && values.email), 107 subject: validateSubject(values && values.subject, subjectRequired), 108 message: validateMessage(values && values.message) 109 }; 110 111 var isValid = !errors.name && !errors.email && !errors.subject && !errors.message; 112 113 var messages = { 114 name: errors.name ? errors.name.message : '', 115 email: errors.email ? errors.email.message : '', 116 subject: errors.subject ? errors.subject.message : '', 117 message: errors.message ? errors.message.message : '' 118 }; 119 120 return { 121 isValid: isValid, 122 errors: errors, 123 messages: messages, 124 rules: RULES 125 }; 126 } 127 128 global.CommonFormValidation = { 129 rules: RULES, 130 validateName: validateName, 131 validateEmail: validateEmail, 132 validateSubject: validateSubject, 133 validateMessage: validateMessage, 134 getLimitMessage: getLimitMessage, 135 validateForm: validateForm 136 }; 137})(window);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.