diff --git a/src/wwwroot/controllers/RegistrationCtrl.js b/src/wwwroot/controllers/RegistrationCtrl.js index 76cf5c0b..13a822dd 100644 --- a/src/wwwroot/controllers/RegistrationCtrl.js +++ b/src/wwwroot/controllers/RegistrationCtrl.js @@ -16,10 +16,11 @@ "Slug", '$location', 'vcRecaptchaService', - 'clerk' + 'clerk', + 'resources' ]; - function RegistrationCtrl($scope, $rootScope, RELAY_CONFIG, signup, utils, $translate, $timeout, Slug, $location, vcRecaptchaService, clerk) { + function RegistrationCtrl($scope, $rootScope, RELAY_CONFIG, signup, utils, $translate, $timeout, Slug, $location, vcRecaptchaService, clerk, resources) { var vm = this; vm.submitRegistration = submitRegistration; vm.emailRegistered = null; @@ -28,6 +29,10 @@ vm.setCaptchaResponse = setCaptchaResponse; vm.setWidgetId = setWidgetId; vm.reloadCaptcha = reloadCaptcha; + vm.regexPhoneNumber = "^\\+?([0-9][\\s-]?(\\([0-9]+\\))*)+[0-9]$"; + vm.regexCountryCode = "^\\+?\\d{1,3}\\s?$"; + vm.regexAreaCode = "^\\(?0?\\d{1,4}\\)?\\s?$"; + vm.regexLocalPhone = "^[\\d]+(?:\\s?[\\d]+){1,5}$"; var customAccountName = false; vm.accountNameUpdated = function () { @@ -41,9 +46,58 @@ var useClerkAuth = RELAY_CONFIG.useClerkAuthentication || false; vm.recaptchaAvailable = !useClerkAuth && !!vcRecaptchaService; + resources.ensureIndustries(); + resources.ensureCountries(); + vm.resources = resources.data; + + function validatePhoneFields() { + if (!$scope.form) { + return true; + } + + var countryModelCtrl = $scope.form.countryPhoneNumber; + var areaModelCtrl = $scope.form.areaPhoneNumber; + var phoneModelCtrl = $scope.form.phoneNumber; + + var countryVal = countryModelCtrl ? (countryModelCtrl.$modelValue || '') : ''; + var areaVal = areaModelCtrl ? (areaModelCtrl.$modelValue || '') : ''; + var phoneVal = phoneModelCtrl ? (phoneModelCtrl.$modelValue || '') : ''; + + var isEmpty = function (value) { + return !value || !value.toString().trim(); + }; + + var countryRegex = new RegExp(vm.regexCountryCode); + var areaRegex = new RegExp(vm.regexAreaCode); + var phoneRegex = new RegExp(vm.regexLocalPhone); + + var isCountryValid = isEmpty(countryVal) ? true : countryRegex.test(countryVal); + var isAreaValid = isEmpty(areaVal) ? true : areaRegex.test(areaVal); + var isPhoneValid = isEmpty(phoneVal) ? true : phoneRegex.test(phoneVal); + + if (countryModelCtrl) { + countryModelCtrl.$setValidity('mask', true); + countryModelCtrl.$setValidity('country_code', isCountryValid); + } + if (areaModelCtrl) { + areaModelCtrl.$setValidity('mask', true); + areaModelCtrl.$setValidity('area_code', isAreaValid); + } + if (phoneModelCtrl) { + phoneModelCtrl.$setValidity('mask', true); + phoneModelCtrl.$setValidity('phone_number_field', isPhoneValid); + } + + return isCountryValid && isAreaValid && isPhoneValid; + } + function submitRegistration(form) { vm.submitted = true; // To show error messages + if (!validatePhoneFields()) { + return; + } + if (useClerkAuth) { validatePasswordConfirmation(); if (form.$invalid) { @@ -72,6 +126,16 @@ origin: $location.search().origin }; + var selectedCountry = $scope.form && $scope.form.country ? $scope.form.country.$modelValue : null; + var selectedIndustry = $scope.form && $scope.form.industry ? $scope.form.industry.$modelValue : null; + var countryCodePart = $scope.form && $scope.form.countryPhoneNumber ? ($scope.form.countryPhoneNumber.$modelValue || '') : ''; + var areaCodePart = $scope.form && $scope.form.areaPhoneNumber ? ($scope.form.areaPhoneNumber.$modelValue || '') : ''; + var phonePart = $scope.form && $scope.form.phoneNumber ? ($scope.form.phoneNumber.$modelValue || '') : ''; + + newUser.country_code = selectedCountry ? selectedCountry.code : null; + newUser.industry_code = selectedIndustry ? selectedIndustry.code : null; + newUser.phone_number = countryCodePart + '-' + areaCodePart + '-' + phonePart; + clerk.signUp(newUser) .then(function (result) { if (result.registered && result.needsVerification) { diff --git a/src/wwwroot/locales/en-translation.js b/src/wwwroot/locales/en-translation.js index af96089f..b2f3ed03 100644 --- a/src/wwwroot/locales/en-translation.js +++ b/src/wwwroot/locales/en-translation.js @@ -139,6 +139,9 @@ "validation_error_email_already_exist": "There is already an user with that email address.", "validation_error_ilegal_date": "Invalid Expiration Date.", "validation_error_mask": "Invalid Format.", + "validation_error_country_code": "Enter a valid country code (1-3 digits).", + "validation_error_area_code": "Enter a valid area code (1-4 digits).", + "validation_error_phone_number_field": "The number you entered doesn’t look valid; please make sure it has enough digits.", "validation_error_invalid_card_number": "Invalid Credit Card number.", "validation_error_payment_failure": "We couldn't process the payment, please check your information.", "validation_error_pattern": "Please check the text", diff --git a/src/wwwroot/locales/es-translation.js b/src/wwwroot/locales/es-translation.js index 26a4f166..7571085b 100644 --- a/src/wwwroot/locales/es-translation.js +++ b/src/wwwroot/locales/es-translation.js @@ -151,6 +151,9 @@ window['relay-translation-es'] = { "validation_error_email_already_exist": "Ya existe un usuario con esa dirección de email.", "validation_error_ilegal_date": "Escribe una fecha de expiración válida.", "validation_error_mask": "Escribe un formato válido.", + "validation_error_country_code": "Ingresa un código de país válido (1-3 dígitos).", + "validation_error_area_code": "Ingresa un código de área válido (1-4 dígitos).", + "validation_error_phone_number_field": "El número ingresado no es válido; asegúrate de que tenga suficientes dígitos.", "validation_error_invalid_card_number": "Escribe una tarjeta de crédito válida.", "validation_error_payment_failure": "No pudimos realizar el pago, por favor revisa tu información.", "validation_error_pattern": "Por favor revisa el texto ingresado", diff --git a/src/wwwroot/partials/signup/registration-w-password.html b/src/wwwroot/partials/signup/registration-w-password.html index 690b2bd9..7a1f99dd 100644 --- a/src/wwwroot/partials/signup/registration-w-password.html +++ b/src/wwwroot/partials/signup/registration-w-password.html @@ -29,6 +29,46 @@