Question
Global Script
Forms require JavaScript for validation and accessibility. To use validation through the global script use the following code. An optional selector targeting the form may be supplied as the first argument to the .validate()
method. The second optional argument is a function that handles the form data after it passes validation.
<form data-js="validate" action="/my/action/" method="post"> <!-- Form questions here --> </form> <script src="@nycopportunity/standard/dist/js/default.js"></script> <script> var Standard = new Default(); Standard.validate(); // or // Accepts a selector string (argument one) and function (argument two) for submission handling Standard.validate('[data-js="validate"]', function(event) { event.preventDefault(); // Add your own custom submission handler here }); </script>
Validation messages will appear when the user…
-
leaves a required question blank,
-
enters an answer doesn’t match the specified input type,
-
enters an answer that does not match a specified pattern,
-
or tries to submit the form without filling out any required fields.
For any of these conditions to be met…
-
the required attribute on the
input
element must be set totrue
, -
or the input needs a valid type attribute value,
-
or the pattern attribute must have a regular expression matching pattern. This is not needed for inputs with a specified type.
Module Import
The form validation source exists in the Patterns Scripts utility library. Install the @nycopportunity/pttrn-scripts
module to import the module. This method allows the specification of watching for input errors on blur, setting the error container’s selector, and other customizations. Refer to the source for details.
import Forms from '@nycopportunity/patterns-scripts/src/forms/forms'; this.form = new Forms(document.querySelector('[data-js="validate"]')); this.form.submit = (event) => { event.preventDefault(); // Submission handler }; this.form.selectors.ERROR_MESSAGE_PARENT = '.c-question__container'; this.form.watch();
The open source library Cleave.js is included as a dependency of the Opportunity Standard to provide input masking support.
Global Script
To initialize supported masking utilities for dollars and phone numbers from the global script use the following code:
<!-- Global Script --> <script src="https://cdn.jsdelivr.net/gh/nycopportunity/standard@v0.0.20/dist/js/default.js"></script> <script> var Standard = new Default(); Standard.masks(); </script>
Module Import
For module imports, import the mask utilities from the source and instantiate masks for dollars and phone numbers
import MaskDollars from '@nycopportunity/standard/src/utilities/mask/mask-dollars.js'; import MaskPhone from '@nycopportunity/standard/src/utilities/mask/mask-phone.js'; new MaskDollars new MaskPhone
Additional masking
To achieve additional masking support, import Cleave.js from the source and pass inputs to instances of Cleave
and customize options.
import Cleave from 'cleave.js'; let inputs = document.querySelectorAll('[data-js="{{ MY_INPUT }}"]'); for (let i = 0; i < inputs.length; i++) { new Cleave(inputs[i], { // Custom Options }); }
More information can be found in the Cleave.js documentation.
Global Script
The Popover requires JavaScript for showing and hiding. To initialize the Popover and Disclaimer instances from the global script use the following code:
<!-- Global Script --> <script src="https://cdn.jsdelivr.net/gh/nycopportunity/standard@v0.0.20/dist/js/default.js"></script> <script> var Standard = new Default(); Standard.popover(); </script>
This will attach event listeners for toggling the Popover.
Module Import
For module imports, import the Popover from the source. You must pass a DOM selection of each Popover to a new instance of the class. A selector reference is stored in the class.
import Popover from '@nycopportunity/standard/src/components/popover/popover'; let elements = document.querySelectorAll(Popover.selector); elements.forEach(element => { new Popover(element); });