Global Functions
Validation Functions - Done
email function validateemail(value) description validates that the provided string is properly formatted as an email address prerequisites before using the validateemail function, ensure that the validator library is installed in your project you can install it using npm npm install validator parameters parameter type description value string the email address to validate return value property type description isvalid boolean true if the email is valid, false otherwise formattedemail string a cleaned up email address formatted as lower case and trimmed of spaces, quotes etc notes library initialization validator instance the function imports the validator js library’s isemail method to perform validation email validation isemail method checks if the provided string is a valid email address based on standard email formatting rules options (optional) the isemail method can accept an options object to fine tune validation parameters, such as allowing specific characters or enforcing stricter rules return value valid email returns { isvalid true } if the email address is valid invalid email returns { isvalid false } if the email address does not conform to standard formats error handling internal handling the isemail method internally handles parsing errors and edge cases, ensuring reliable validation without the need for explicit error handling in the function example usage description input output valid email address validateemail("john doe\@example com") { isvalid true } invalid email address validateemail("john doe\@ com") { isvalid false } email with subdomain validateemail("john doe\@mail example co uk") { isvalid true } email with plus sign validateemail("john doe+newsletter\@example com") { isvalid true } email with invalid characters validateemail("john doe\@exam!ple com") { isvalid false } empty string validateemail("") { isvalid false } script / validates an email address using the validator js library @param {string} value the email address to validate @returns {{ isvalid boolean, validationerror? string }} an object containing the validity of the email and an optional validation error message / function validateemail(value) { const validator = require('validator'); const isvalid = validator isemail(value trim()); if (isvalid) { return { isvalid }; } else { return { isvalid false, validationerror 'invalid email address format ', }; } } url function validateurl(value) description the validateurl function is designed to validate and format a given url string it ensures that the url is correctly structured, optionally removes url parameters, and normalizes the protocol to https // if it’s absent this function supports both http and https protocols and returns an object indicating the validity of the url along with the formatted url string parameters parameter type description value string the url string to be validated and formatted removeparams boolean (optional) a flag indicating whether to remove url parameters (query string) from the url defaults to false if not provided return value parameter type description isvalid boolean indicates whether the input url is valid (true) or invalid (false) formattedurl string or null the formatted url string if valid, all lower case, trimmed; otherwise, null notes trimming input removes any leading and trailing whitespace from the input value to ensure accurate validation protocol handling checks if the input url starts with http // or https // (case insensitive) if the protocol is missing, it defaults to adding https // to promote secure urls url parsing and validation utilizes the built in url constructor to parse the url if the url constructor successfully parses the url, it’s considered valid if parsing fails (i e , the url is invalid), the function catches the error and marks the url as invalid formatting trailing slashes removes any trailing slashes from the url’s pathname to standardize the format url parameters if removeparams is true, it strips away the query parameters (?query=123) from the url hash preservation preserves the hash (#section) if present in the original url error handling employs try catch blocks to gracefully handle invalid urls without causing runtime errors example usage input output validateurl(" example com/ ") { isvalid true, formattedurl "https //example com" } validateurl("https //example com/path?query=123", true) { isvalid true, formattedurl "https //example com/path" } validateurl("ht!tp\ //invalid url") { isvalid false, formattedurl null } script / validates and formats a given url string @param {string} value the url string to be validated and formatted @param {boolean} \[removeparams=false] optional flag to remove url parameters (query string) @returns {{ isvalid boolean, formattedurl string | null, validationerror? string }} an object containing the validity of the url, the formatted url string, and an optional validation error message / function validateurl(value, removeparams = false) { let trimmedvalue = value trim(); // ensure the url starts with http // or https // if (!/^https? \\/\\//i test(trimmedvalue)) { trimmedvalue = 'https //' + trimmedvalue; } try { const url = new url(trimmedvalue); let formattedurl = `${url protocol}//${url host}${url pathname replace(/\\/+$/, '')}`; if (!removeparams && url search) { formattedurl += url search; } if (url hash) { formattedurl += url hash; } return { isvalid true, formattedurl, }; } catch (error) { // determine a user friendly error message let errormessage = 'invalid url format '; // optionally, use the error message from the url constructor if (error instanceof typeerror) { errormessage = error message; } return { isvalid false, formattedurl null, validationerror errormessage, }; } }