Michael Orji
SmartFormValidator
is now available as a property of the global object.SmartFormValidator
is now available as a property of the global object.script
tag:input
, checkbox
, textarea
, select
, and contenteditable
elements. A field must have an id
property. A field may optionally have a role
and getValue
properties.button
or input
whose type
is not submit
but on which we want effects that handle submit buttons to have access, a role
attribute with the value of submit-button
is required for that field.getValue
property, if provided, must be a function. This function is used to get the current value of the field if the field is not a contenteditable
field and is not one of input
, checkbox
, textarea
, or select
.field
. The value of the field
can be either the element's id
or, in instances where we have previously obtained a reference to the element, the element reference.type
{String}: specifies the acceptable data type of the value for this field, one of either ("alnum"
|"alpha"
|"ascii"
|"email"
|"number"
). Default is alnum
.required
{Boolean}: specifies whether the field is required (true
) or not (false
).length
{Number|Object}: specifies the accepted (minimum or maximum or both) input length. If the value is a number, it specifies the maximum length. If the value is an object, it specifies. the minimum and/or maximum length: matchCase
{Boolean}: performs a case-sensitive (true) or case-insensitive (false) validation.allowWhitespace
{Boolean}: specifies whether the field allows whitespace values or not.regex
{Object|String}: specifies a custom validation regex. This can be a regex string or regex object.alphaValidator
: checks that a field contains only the letters A - Z
, underscores (_
), and dashes (-
). The field can also accept whitespace characters with the allowWhitespace
constraint set to true
. To perform a case-insensitive check, set the matchCase
constraint to false
.alphanumericValidator
: checks that a field contains only the letters A - Z
, the numbers 0 - 9
, underscores (_
), and dashes (-
). The field can also accept whitespace characters with allowWhitespace
set to true. To perform a case-insensitive check, set matchCase
to false
.asciiTextValidator
: checks that a field contains only characters from the ASCII character-set. This validator accepts whitespace characters by default irrespective of the value of allowWhitespace
. However, we can specify a case-insensitive match by setting matchCase
to false
.emailValidator
: checks that the value entered into the field has a valid email format.lengthValidator
: checks that the value entered in the field is between the minimum and/or maximum specified length.numberValidator
: checks that a field contains only the numbers 0 - 9
. The field can also accept whitespace characters with allowWhitespace
set to true.regexValidator
: checks that a field's value conforms to a custom regular expression constraint.requiredFieldValidator
: checks that a field has some data entered into it.value
: the data entered by the user into the field.rule
: an object containing the constraints specified for that field. The validator checks that the value
adheres to the rule.prevResult
: a boolean value that tells the current validator the result of the previous validator in the chain of validators attached to this field.extras
: an object containing any extra information. A checkbox, for example, can have the value on
or off
and a checked
state that can be either true
or false
. The value (on
or off
) will be passed as the first argument (the value
argument) to the validator. The checked
state will be passed in as extras.checked
.true
if the field it is validating passed the validation rule. Otherwise it should return false
.addValidator
method of the SmartFormValidator
instance.addValidator
takes three ordered arguments:name
{String} (required): an arbitrary string that can serve as a unique name for the validator.validator
{Function} (required): the validator function itself.meta
{Object} (optional): an object that holds meta information about the validator. To prevent naming conflict with other validators, we can have a meta.namespace
property. This property should be a string. This string is used in conjunction with the validator name
to create a unique name for the validator.required
state, a maximum length constraint, and whether or not the field contains numbers, it's better to have separate validators for each of these checks: one to check for required
, another to ensure the length
constraint is met, and yet another to check that the number
constraint is fulfilled. Each of these validators will be called with the rule and the result of the previous validator.true
or false
values. A validator should not throw errors nor otherwise perform a side-effect on a field in the event of a successful or failed validation. Any such effects should be delegated to effects.name
{String}: the effect name is an arbitrary string used to uniquely identify the effect.valid
{Function}: a function to be invoked to handle the case when the field passes validation. The function is passed the field as the first argument.invalid
{Function}: a function to be called to handle the case when the field fails validation. The function is passed the field as the first argument.init
{Function}: an initialization function that is called to perform any initialization tasks for the effect. This function is called once when the effect is registered.meta
{Object}: an object that holds meta information (such as author, version, etc) about the effect. A recommended property is meta.namespace
that helps to prevent naming conflict with other effects. This property expects a string value that is used in conjunction with the effect name
property to create a unique name for the effect.SmartFormValidator
. Just call useEffect(effect)
statically on the SmartFormValidator
class like so: SmartFormValidator.useEffect(effect)
.SmartFormValidator
instance on which useEffect
is called. To do a "local" effect registration, call the useEffect(effect)
method on an instance. For example:useEffect
expects the complete effect object as its argument.field
property that specifies the target field. However,
we may omit this property while creating the rule, but must specify it when adding the rule to the field.npm run examples
git checkout main
.git merge develop
.npm run build
.git add dist
.git commit -m ":rocket: Build latest version"
.npm run release:[major|minor|patch]
.package.json.version
) and make any necessary adjustments.git add CHANGELOG.md package*.json
git commit -m ":books: Update CHANGELOG.md, and :package: bump version number from <prev_ver> to <curr_ver>"
.git tag -a vx.x.x -m ":bookmark: <tag summary>"
.git push && git push origin --tags
.npm publish
.git checkout develop
.git merge main
.npm i smart-form-validator