Skip to content

Commit

Permalink
Merge pull request #151 from unicef/feature/add-regex-validation-to-t…
Browse files Browse the repository at this point in the history
…ext-validation

PR to add matchRegexpCaseInSensitive rule to ValidatorForm
  • Loading branch information
dfrancisc authored Mar 5, 2024
2 parents 566ea65 + a871fee commit cc448cb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
14 changes: 14 additions & 0 deletions example/src/components/FormValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default function FormValidator() {
const [values, setValues] = useState({
currency: '',
email: '[email protected]',
regex: '',
password: 'testinghere',
positiveInteger: 1,
currencyField: 1000.55,
Expand Down Expand Up @@ -325,6 +326,19 @@ export default function FormValidator() {
value={values.safeText}
/>
</Grid>
<Grid item xs={12} lg={3} xl={2}>
<UTextField
label="Custom regex(no script)"
onChange={handleValue}
className={classes.margin}
name="regex"
variant="outlined"
validators={[
'matchRegexpCaseInSensitive:^(?!.*<script\\b[^>]*>.*<\\/script\\s*>).*$',
]}
value={values.regex}
/>
</Grid>
<Grid item xs={12} lg={3} xl={2}>
<UTextField
label="Alphanumeric"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unicef/material-ui",
"version": "0.12.2",
"version": "0.12.3",
"description": "UNICEF theme and components of material-ui for react",
"main": "index.js",
"files": [
Expand Down
8 changes: 8 additions & 0 deletions src/components/UValidatorForm/UValidatorForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types'
import {
isAlphanumericText,
isPhoneNumberText,
isRegexCaseInSensitive,
isSafeText,
isUrlText,
} from '../../utils'
Expand Down Expand Up @@ -54,6 +55,13 @@ function ForwardRefForm(props, ref) {
return isAlphanumericText(value)
})

ValidatorForm.addValidationRule(
'matchRegexpCaseInSensitive',
(value, regexStr) => {
return isRegexCaseInSensitive(value, regexStr)
}
)

return <ValidatorForm {...props} ref={ref} />
}

Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isSafeText,
isAlphanumericText,
isUrlText,
isRegexCaseInSensitive,
} from './validationRules'

export {
Expand All @@ -13,4 +14,5 @@ export {
isSafeText,
isAlphanumericText,
isUrlText,
isRegexCaseInSensitive,
}
14 changes: 14 additions & 0 deletions src/utils/validationRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cc448cb

Please sign in to comment.