1const VALIDATION = { 2 TEXT_EMPTY: { REGEX: new RegExp(/^(?!\s*$).+/), MESSAGE: 'This is a required field' }, 3 EMAIL_ADDRESS: { 4 REGEX: new RegExp(/^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/), 5 REGEX_MESSAGE: 'Please enter a valid email', 6 REQUIRED_MESSAGE: 'Email address field is required.', 7 }, 8 PHONE_NUMBER: { 9 REGEX: new RegExp(/^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/), 10 REGEX_MESSAGE: 'Please enter a valid phone number', 11 REQUIRED_MESSAGE: 'Phone field is required.', 12 }, 13 URL_PATH: { 14 REGEX: new RegExp(/^(?!\/|www\.|.*https?\:|.*\.com?\/|.*\.ca\/|.*\.org\/|.*\.net\/|.*\.edu\/|.*\.us\/|.*\.in\/|.*\.io\/|.*\\|.*\/\/).*/), 15 REGEX_MESSAGE: 'Please enter a valid URL path', 16 REQUIRED_MESSAGE: 'URL path is required', 17 }, 18 CLIENT_TYPES: { 19 MESSAGE: 'Please select: Buyer, Seller, Renter or Other.', 20 }, 21 EXCEMPT_FORMS: ['comment_form','foreclosure_listing_form_landing','get_your_dream_commercial_property_form','commercial_property_evaluation_form'] 22}; 23 24const EMAIL_SELECTOR = "input#edit-mail,input#edit-email,input#edit-emailF,input[type='email']#email,input[type='text']#email"; 25const PHONE_SELECTOR = "input#edit-phone,input#edit-phoneF,input[type='text']#phone"; 26const PATH_SELECTOR = ".tab-content fieldset.path-fields input#edit-path"; 27 28 29function addErrorMessage(el, message) { 30 if (!el.attr('data-error')) { 31 el.attr('data-error', true); 32 if(el.closest('form#pre_construction_landing_form').length > 0 || el.closest('form#properties_landing_form').length > 0){ 33 el.addClass('error'); 34 el.after(`<label id="edit-email-error" class="error" for="edit-email">${message}</label>`); 35 }else{ 36 el.after(`<small class="error" style="color:red">${message}</small>`); 37 38 } 39 } 40} 41 42function removeErrorMessage(el) { 43 el.next('small.error').remove(); 44 el.removeAttr('data-error'); 45} 46 47function is_required_field(el) { 48 return el.siblings('label').children('span.req,span.form-required,span.asterisk').length; 49} 50 51function is_shown(el) { 52 return el.is(":visible") && !el.is(":hidden"); 53} 54 55function valid_phone_number(phone) { 56 return VALIDATION.PHONE_NUMBER.REGEX.test(phone); 57} 58 59function valid_email_address(email) { 60 return VALIDATION.EMAIL_ADDRESS.REGEX.test(email); 61} 62 63function valid_text(text) { 64 return VALIDATION.TEXT_EMPTY.REGEX.test(text); 65} 66 67function valid_url_path(path) { 68 return VALIDATION.URL_PATH.REGEX.test(path); // regex test for url paths 69} 70 71function valid_client_type_selection(el) { 72 return ( 73 !el.hasClass('buySell') && 74 !el.hasClass('sell') && 75 !el.hasClass('buy') && 76 !el.hasClass('rent') && 77 !el.hasClass('other') 78 ); 79} 80 81function removeAllErrors(form) { 82 $('small.error', form).remove(); 83 $('[data-error]', form).each((i, e) => $(e).removeAttr('data-error')); 84} 85 86function handleSubmission(event) { 87 let errors = []; 88 const context = $(this).parents('form'); 89 const phoneNumber = $(PHONE_SELECTOR, context); 90 const email = $(EMAIL_SELECTOR, context); 91 const path = $(PATH_SELECTOR, context); 92 const clientType = $('.drip-group-container', context); 93 94 if ( !VALIDATION.EXCEMPT_FORMS.includes(context.attr('id')) ) { 95 removeAllErrors(context); 96 97 if (email.length) { 98 if (is_required_field(email) && !valid_text(email.val())) { 99 errors.push({ 100 el: email, 101 message: VALIDATION.EMAIL_ADDRESS.REQUIRED_MESSAGE, 102 }); 103 } else if (valid_text(email.val()) && !valid_email_address(email.val())) { 104 errors.push({ 105 el: email, 106 message: VALIDATION.EMAIL_ADDRESS.REGEX_MESSAGE, 107 }); 108 } 109 } 110 if (phoneNumber.length) { 111 if (is_required_field(phoneNumber) && !valid_text(phoneNumber.val())) { 112 errors.push({ 113 el: phoneNumber, 114 message: VALIDATION.PHONE_NUMBER.REQUIRED_MESSAGE, 115 }); 116 } else if (valid_text(phoneNumber.val()) && !valid_phone_number(phoneNumber.val())) { 117 errors.push({ 118 el: phoneNumber, 119 message: VALIDATION.PHONE_NUMBER.REGEX_MESSAGE, 120 }); 121 } 122 } 123 if(path.length) { 124 if (is_required_field(path) && !valid_text(path.val())) { 125 errors.push({ 126 el: path, 127 message: VALIDATION.URL_PATH.REQUIRED_MESSAGE, 128 }); 129 } else if (valid_text(path.val()) && !valid_url_path(path.val())) { 130 errors.push({ 131 el: path, 132 message: VALIDATION.URL_PATH.REGEX_MESSAGE, 133 }); 134 } 135 } 136 137 if (clientType.length && is_shown(clientType)) { 138 if (valid_client_type_selection(clientType)) { 139 errors.push({ 140 el: clientType, 141 message: VALIDATION.CLIENT_TYPES.MESSAGE, 142 }); 143 } 144 } 145 } 146 147 if (errors.length) { 148 event.preventDefault();
149 errors.forEach((error) => { 150 addErrorMessage(error.el, error.message); 151 }); 152 } 153} 154 155function handleUserInputValidation(e) { 156 const target = $(e.target); 157 158 removeErrorMessage(target); 159 160 if (target.val().length < 5) { 161 return; 162 } 163 164 if (target.attr('id').includes('phone')) { 165 if (!valid_phone_number(target.val())) { 166 addErrorMessage(target, VALIDATION.PHONE_NUMBER.REGEX_MESSAGE); 167 } 168 } 169 if (target.attr('id').includes('mail')) { 170 if (!valid_email_address(target.val())) { 171 addErrorMessage(target, VALIDATION.EMAIL_ADDRESS.REGEX_MESSAGE); 172 } 173 } 174 if(target.attr('id').includes('path')) { 175 if (!valid_url_path(target.val())) { 176 addErrorMessage(target, VALIDATION.URL_PATH.REGEX_MESSAGE); 177 } 178 } 179} 180$(document).ready(function () { 181 $('form#node-form .tab-content fieldset.path-fields legend').append(':<span class="form-required" title="This field is required."> *</span>'); 182 $('form').each(function () { 183 $(PHONE_SELECTOR, this).on('input', handleUserInputValidation); 184 $(EMAIL_SELECTOR, this).on('input', handleUserInputValidation); 185 $(PATH_SELECTOR, this).on('input', handleUserInputValidation); 186 $('[type="submit"]', this).on('click', handleSubmission); 187 }); 188});
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.