diff --git a/example/src/components/FormValidator.js b/example/src/components/FormValidator.js index b9027ea9a..49576172a 100644 --- a/example/src/components/FormValidator.js +++ b/example/src/components/FormValidator.js @@ -78,6 +78,7 @@ export default function FormValidator() { const [values, setValues] = useState({ currency: '', email: 'test@test.com', + regex: '', password: 'testinghere', positiveInteger: 1, currencyField: 1000.55, @@ -325,6 +326,19 @@ export default function FormValidator() { value={values.safeText} /> + + ]*>.*<\\/script\\s*>).*$', + ]} + value={values.regex} + /> + { + return isRegexCaseInSensitive(value, regexStr) + } + ) + return } diff --git a/src/utils/index.js b/src/utils/index.js index 6d3526192..45ccebdb9 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -4,6 +4,7 @@ import { isSafeText, isAlphanumericText, isUrlText, + isRegexCaseInSensitive, } from './validationRules' export { @@ -13,4 +14,5 @@ export { isSafeText, isAlphanumericText, isUrlText, + isRegexCaseInSensitive, } diff --git a/src/utils/validationRules.js b/src/utils/validationRules.js index 70742a1ee..8c34ee3c6 100644 --- a/src/utils/validationRules.js +++ b/src/utils/validationRules.js @@ -12,6 +12,20 @@ export function isPhoneNumberText(value) { return true } +/** + * + * @param {String} value Value text to be validated + * @returns {Boolean} Whether the given value string contains safe text (no html tags) + */ +export function isRegexCaseInSensitive(value, regexRule) { + const valueToTest = value || '' + const regexStr = regexRule + if (!regexStr) return true // if no regex, then accept any text + + const regex = new RegExp(regexStr, 'i') + return regex.test(valueToTest) +} + /** * * @param {String} value Value text to be validated