const validateEmail = email =>
new RegExp(/^[^\s@]+@[^\s@]+\.[^\s@]+$/).test(email)
const validateCurrency = currency =>
new RegExp(/^\d+(?:\.\d{0,2})?$/).test(currency)
const validatePostcode = postcode =>
new RegExp(/^[a-z]{1,2}\d[a-z\d]?\s*\d[a-z]{2}$/i).test(postcode)
const validateUkLandline = ukLandline =>
new RegExp(/^0[12][0-9]{9}$/).test(ukLandline)
const validateUkMobile = ukMobile => new RegExp(/^07[0-9]{9}$/).test(ukMobile)
const validateUkAny = ukAny => new RegExp(/^0[127][0-9]{9}$/).test(ukAny)
const validateAnyPhoneNumber = anyPhoneNumber =>
new RegExp(/^([0-9]{0,16})$/).test(anyPhoneNumber)
const validateUrl = url =>
new RegExp('^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?').test(
url
)
Alternatively, we could use the URL API here:
const validateUrl = url => {
try {
return Boolean(new URL(url))
} catch {
return false
}
}
const validateDate = date =>
new RegExp(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/).test(date)
const toKebabCase = value =>
value
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[^\w ]+/g, '')
.replace(/ +/g, '-')
.toLowerCase()