Skip to content

Instantly share code, notes, and snippets.

@maxinspace
Created January 15, 2017 16:27
Show Gist options
  • Select an option

  • Save maxinspace/568368f3d4aba195d463e061841aee27 to your computer and use it in GitHub Desktop.

Select an option

Save maxinspace/568368f3d4aba195d463e061841aee27 to your computer and use it in GitHub Desktop.
TestTask JS
function solution() {
const validationSelect = $("input:radio:checked").val();
switch (validationSelect) {
case "person":
return validatePerson();
break;
case "company":
return validateCompany();
break;
default:
return false
};
};
function validatePerson() {
return validatePresenceOf("#first_name") &&
validatePresenceOf("#last_name") &&
validateEmail("#email")
};
function validateCompany() {
return validateCompanyName() &&
validatePhone("#phone")
};
function validatePresenceOf(fieldSelector) {
const fieldValue = $(fieldSelector).val();
return (fieldValue == null || fieldValue == "")
};
function validateCompanyName() {
const fieldValue = $("#company_name").val();
return (fieldValue == null )
};
function validateEmail(emailSelector) {
const emailValue = $(emailSelector).val();
const emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return !!emailReg.test( emailValue );
};
function validatePhone(phoneSelector) {
const phoneValue = $(phoneSelector).val();
const phoneReg = /^(\d{3}\.\d{3}\.\d{3})$/;
return !!phoneReg.test( phoneValue );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment