Created
February 3, 2018 11:42
-
-
Save SkyzohKey/3debc00cdcabe246b1b6a174a97bd188 to your computer and use it in GitHub Desktop.
A simple excersice of validating a form with vanilla JS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
<form> | |
<input type="radio" id="type_person" name="type" value="person" checked/> | |
<input type="radio" id="type_company" name="type" value="company"/> | |
<input type="text" id="first_name" name="first_name" value="John"/> | |
<input type="text" id="last_name" name="last_name" value="Doe"/> | |
<input type="text" id="email" name="email" value="[email protected]"/> | |
<input type="text" id="company_name" name="company_name" value=""/> | |
<input type="text" id="phone" name="phone" value="234-567-890"/> | |
</form> | |
**/ | |
const solution = () => { | |
const personFields = [ | |
{ id: "first_name", rule: "not-empty" }, | |
{ id: "last_name", rule: "not-empty" }, | |
{ id: "email", rule: "email-address" } | |
]; | |
const companyFields = [ | |
{ id: "company_name", rule: "not-empty" }, | |
{ id: "phone", rule: "phone-number" } | |
]; | |
const typePerson = document.querySelector("#type_person"); | |
const typeCompany = document.querySelector("#type_company"); | |
if (typePerson.checked && !typeCompany.checked) { | |
// Validate first_name, last_name & email. | |
return validateFields(personFields).allValid; | |
} else { | |
// Validate company_name & phone. | |
return validateFields(companyFields).allValid; | |
} | |
}; | |
/** | |
* Given an array of fields, return true if all the fields are valid. | |
* It also does returns the fields that were checked to allow for later displaying errors to the user. | |
* | |
* @param {Array<Object>} fields - An array containing fields as object (ie. `{ id: '', rule: '' }`). | |
* @returns {Array<Object>} Returns an array of object with results for every fields validated. | |
*/ | |
const validateFields = fields => { | |
const fieldsResults = { | |
fields: [], | |
allValid: false, | |
valid: 0, | |
invalid: 0 | |
}; | |
for (let i = 0, l = fields.length; i < l; i++) { | |
const field = fields[i]; | |
const el = document.querySelector(`#${field.id}`); | |
const isValid = validateRule(field.rule, el.value); | |
fieldsResults.fields.push({ | |
id: field.id, | |
validated: isValid, | |
rule: field.rule | |
}); | |
if (isValid) { | |
fieldsResults.valid += 1; | |
} else { | |
fieldsResults.invalid += 1; | |
} | |
} | |
fieldsResults.allValid = fieldsResults.invalid === 0; | |
return fieldsResults; | |
}; | |
/** | |
* Given a rule and a value, validate the value. | |
* | |
* @param {String} ruleName - A rule name used to validate the value against. | |
* @param {String} value - The value to check. | |
* @returns {boolean} Returns a Boolean value that indicate whether or not a value passes a rule check. | |
*/ | |
const validateRule = (ruleName, value) => { | |
switch (ruleName) { | |
case "not-empty": | |
return String(value).trim() !== ""; | |
case "email-address": | |
const emailRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; | |
return emailRegex.test(value); | |
case "phone-number": | |
const phoneRegex = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/; | |
return phoneRegex.test(value); | |
default: | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment