npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

formfortify

v1.1.0

Published

FormFortify is a JavaScript library designed to assist you with form validation and error handling. It provides functions for registering form fields for validation and fortifying forms for submission using custom validation rules.

Downloads

18

Readme

FormFortify

FormFortify is a JavaScript library designed to assist you with form validation and error handling. It provides functions for registering form fields for validation and fortifying forms for submission using custom validation rules.

Usage

  1. Fortifying a Form: Before registering any form fields, you must fortify the form itself using the fortify function.

    Example:

    const formElement = document.querySelector("form#exampleForm");
    fortify(formElement, submitHandler);
  • formElement is the form element you want to fortify for submission.
  • submitHandler is the function that will handle the form submission if all fields pass validation.
  1. Registering a Form Field: To register a form field, use the register function, passing the field element as an argument.

    Example:

    const inputElement = document.querySelector("input#exampleField");
    
    // Register the input field with a unique name and optional validation rules
    register(inputElement, "fieldName", {
      require: true, // Example validation rule
      maxLength: 10, // Another example rule
    });
  • inputElement: The form field element you want to register.
  • "fieldName": A unique name for the field.
  • Validation Rules: The object passed as the third parameter contains validation rules. In this example, we're using "require" and "maxLength."

    Note: Before registering any form field, make sure it is placed inside a fortified form.

  1. Associating an Error Element: First, create an error element in your HTML that will display the error message. You can select this element in your JavaScript code using document.querySelector or a similar method.

    Example:

    const errorElement = document.querySelector(".error-message");
    
    // Associate an error element with a registered field
    registerError(errorElement, "fieldName");
  • errorElement is the element where you want to display error messages.
  • "fieldName" is the name of the registered form field you want to associate with this error message.

    Note: Before registering any error element, ensure that the associated form field has been registered.

Form Submission

The library prevents form submission if there are validation errors. The form submission is handled by the submitHandler function. If all form fields are valid, the submitHandler function is called with the form data as an argument.

Validation Rules

In this library, you can use various validation rules to validate form fields. These rules are specified when registering a form field for validation. Below, we provide a description for each of the available validation rules:

  • require:

    • Description: This rule is used to check if a field is required or not. It can be used for fields that must have a non-empty value.
    • Example:
      register(inputElement, "fieldName", { require: true });
  • maxLength:

    • Description: This rule validates that the length of the field's value does not exceed a specified maximum length.
    • Example:
      register(inputElement, "fieldName", { maxLength: 10 });
  • minLength:

    • Description: This rule validates that the length of the field's value is at least a specified minimum length.
    • Example:
      register(inputElement, "fieldName", { minLength: 5 });
  • max:

    • Description: This rule is used to validate the maximum value for numeric fields. It ensures that the field's value does not exceed a specified maximum.
    • Example:
      register(inputElement, "age", { max: 100 });
  • min:

    • Description: This rule is used to validate the minimum value for numeric fields. It ensures that the field's value is not less than a specified minimum.
    • Example:
      register(inputElement, "age", { min: 18 });
  • custom:

    • Description: This rule allows you to define custom validation logic for a field. It accepts a custom validation function that should return a boolean indicating the validation result.

    • Example:

      register(inputElement, "customField", { custom: customValidator });
      // Example of a custom validation function
      function customValidator(value) {
        // For example, let's say you want to ensure that the input contains at least one uppercase letter
        const containsUppercase = /[A-Z]/.test(value);
      
        return containsUppercase;
      }

License

This library is open-source and provided under the MIT license. Please refer to the LICENSE file for detailed information regarding the terms and conditions of use, modification, and distribution.