$(document).ready(function () { handleEvents(); }); function handleEvents() { // Prevent typing '+' character in phone number field $('#phoneNumber').on('keypress', function (e) { if (e.key === '+') { e.preventDefault(); } }); $("#btnVerify").on("click", function (e) { e.preventDefault(); const firstName = $('#firstName').val().trim(); const lastName = $('#lastName').val().trim(); const email = $('#email').val().trim(); const phoneNumber = $('#phoneNumber').val().trim(); const accountNumber = $('#accountNumber').val().trim(); const password = $('#password').val(); const confirmPassword = $('#confirmPassword').val(); let isValid = true; $('#createAccountModal input').removeClass('is-invalid'); $('#passwordError').hide(); $('#confirmPasswordError').hide(); // Validate fields const namePattern = /^[A-Za-z]+$/; if (firstName === '') { $('#firstName').addClass('is-invalid'); $('#firstName').next('.invalid-feedback').text('First name is required.'); isValid = false; } else if (!namePattern.test(firstName)) { $('#firstName').addClass('is-invalid'); $('#firstName').next('.invalid-feedback').text('First name must contain only letters (no spaces, numbers, or special characters).'); isValid = false; } if (lastName === '') { $('#lastName').addClass('is-invalid'); $('#lastName').next('.invalid-feedback').text('Last name is required.'); isValid = false; } else if (!namePattern.test(lastName)) { $('#lastName').addClass('is-invalid'); $('#lastName').next('.invalid-feedback').text('Last name must contain only letters (no spaces, numbers, or special characters).'); isValid = false; } if (email === '') { $('#email').addClass('is-invalid'); isValid = false; } else if (!validateEmail(email)) { $('#email').addClass('is-invalid'); isValid = false; } // Require exactly 10 digits, no extension code let phonePattern = /^\d{10}$/; if (phoneNumber === '') { $('#phoneNumber').addClass('is-invalid'); $('#phoneNumberError').show().text('Business phone number is required and must be 10 digits.'); isValid = false; } else if (!phonePattern.test(phoneNumber)) { $('#phoneNumber').addClass('is-invalid'); $('#phoneNumberError').show().text('Please enter a valid 10-digit phone number.'); isValid = false; } else { $('#phoneNumber').removeClass('is-invalid').addClass('is-valid'); $('#phoneNumberError').hide(); } if (accountNumber === '') { $('#accountNumber').addClass('is-invalid'); isValid = false; } // Password validation const passwordRequirements = [ { regex: /.{8,}/, message: "at least 8 characters" }, { regex: /[A-Z]/, message: "one uppercase letter" }, { regex: /[a-z]/, message: "one lowercase letter" }, { regex: /[0-9]/, message: "one number" }, { regex: /[^A-Za-z0-9]/, message: "one special character" } ]; let passwordErrors = []; if (!password) { passwordErrors.push("Password is required."); } else { passwordRequirements.forEach(req => { if (!req.regex.test(password)) { passwordErrors.push("Password must contain " + req.message + "."); } }); } if (passwordErrors.length > 0) { $('#password').addClass('is-invalid'); $('#passwordError').show().html(passwordErrors.join('
')); isValid = false; } if (!confirmPassword) { $('#confirmPassword').addClass('is-invalid'); $('#confirmPasswordError').show().text('Confirm password is required.'); isValid = false; } if (password && confirmPassword && password !== confirmPassword) { $('#password').addClass('is-invalid'); $('#confirmPassword').addClass('is-invalid'); $('#confirmPasswordError').show().text('Passwords do not match.'); isValid = false; } if (!isValid) { return; } const payload = { AccountNumber: accountNumber, Email: email, PhoneNumber: phoneNumber, FirstName: firstName, LastName: lastName, Password: password }; verifyAccount(payload); }); $("#btnCancel").on('click', function () { clearForm(); $('#createAccountModal input').removeClass('is-invalid'); }); $('#createAccountModal').on('hidden.bs.modal', function () { clearForm(); $('#createAccountModal input').removeClass('is-invalid'); }); } function validateEmail(email) { // Simple regex pattern for email validation const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return re.test(email); } function verifyAccount(data) { showLoader(); let payload = {}; payload.eventData = JSON.stringify(data); shell.ajaxSafePost({ type: "POST", contentType: "application/json", url: "/_api/cloudflow/v1.0/trigger/5d97494d-1ba1-f011-bbd3-6045bd088d91", processData: false, data: JSON.stringify(payload), global: false, }) .done(function (res) { hideLoader(); let response; try { response = JSON.parse(res); } catch (e) { console.error("Failed to parse response:", e); showErrorToast("Invalid response"); return; } if (document.activeElement) { document.activeElement.blur(); } if (response.status != "Contact exists" && response.status != "No account exists" && response.status != "Something went wrong") { // Only close the modal on success const modalEl = document.getElementById('createAccountModal'); const modal = bootstrap.Modal.getInstance(modalEl) || new bootstrap.Modal(modalEl); modal.hide(); $('.modal-backdrop').remove(); $('body').removeClass('modal-open'); clearForm(); // Show success modal const successModalEl = document.getElementById('successModal'); if (window.setSuccessModalLoginHint) { window.setSuccessModalLoginHint(data.Email); } setTimeout(() => { successModalEl.removeAttribute('aria-hidden'); successModalEl.style.display = ''; let successModal = bootstrap.Modal.getInstance(successModalEl); if (!successModal) { successModal = new bootstrap.Modal(successModalEl, { backdrop: 'static', keyboard: true }); } successModal.show(); const focusTarget = successModalEl.querySelector('button, h1, [tabindex]'); if (focusTarget) focusTarget.focus(); }, 200); } else { // Do NOT close or reset the modal on error if (response.status === "Contact exists") { showErrorToast("You already have an account."); } else if (response.status === "No account exists") { showErrorToast("Account number is invalid."); } else if (response.status === "Something went wrong") { showErrorToast("Something went wrong. Please try again later."); } else { showErrorToast("Something went wrong. Please try again later."); } } }) .fail(function (error) { hideLoader(); showErrorToast("failure"); console.log(error); // Do not close or reset the modal on failure }) } function clearForm() { $('#createAccountModal input').val(''); }