A tiny vanilla JS library for validating form elements.
The syntax is straight forward. Every element that needs to be validated
should have the same class name, a data-regex value and a
data-alert value.
data-regex accepts any valid JS regular expression as a string. So if
you'd need to validate a form input that accepts 0 or more characters,
set data-regex to .*, and for 1 or more characters, set it to .+.
data-alert is the statement that will be set as the placeholder value
of the validating form element, after clearing out the entered value, in
case validation fails.
The library also has the functionality to validate passwords. Just use
the same class name across all the elements that need validation, and in
case Validate-Form comes across <input type="password">, it
automatically ensures that the password entered matches any other
<input type="password"> that it might come across in the page.
var validated = validate_form(form_id, form_elements_class);
validate_form accepts 2 parameters -->
-
form_id, which is theidof the elements' enclosingdiv, orform. It doesn't matter if the enclosing section is ap,span, adivor aform, since the library only queries for all elements under the sameid. -
form_elements_class, which is the class assigned to all the elements that need to be validated. Under the hood,validate_formusesgetElementsByClassName, so all the elements in a form need to have the same class name.