2.0.0 • Published 5 months ago

formoosejs v2.0.0

Weekly downloads
-
License
GNU General Publi...
Repository
github
Last release
5 months ago

Formoose

Formoose is a lightweight, flexible JavaScript library for form validation. It supports a wide range of validation rules, including custom rules, and is designed to be easy to use and extend.

Features

  • Rich validation rules: includes common rules like email, passwords, numbers, dates, checkbox, and radio groups.
  • Custom Messages: Easily set custom error messages for each rule.
  • Grouped Input Support: Validate grouped inputs like radio buttons and checkboxes.
  • Event Based: Custom events (formoose:submitting, formoose:submitted) to integrate validation seamlessly with your workflow.
  • Easy Error Display: Automatically displays error messages next to invalid inputs.
  • Highly Configurable: Customize or extend rules to meet specific requirements.

Installation

npm i formoosejs

Alternatively, you can clone the repository and put the dist directory in your project.

Build

The dist directory contains two pre-built versions of Formoose to accommodate different module systems and environments:

1. formoose.es.js

This build is an ES Module (ESM) version of Formoose, designed for modern JavaScript environments that support the import and export syntax natively.

  • Use Case: Recommended for modern browsers and build tools like Webpack, Vite, or Rollup that handle ESM modules efficiently.
  • How to Use: Import it directly into your JavaScript project:
  import Formoose from './dist/formoose.es.js';

  const formoose = new Formoose();

2. formoose.umd.js

This build is a Universal Module Definition (UMD) version, which works in both browser and Node.js environments. It is compatible with both CommonJS (require) and AMD module systems, as well as directly in <script> tags.

  • Use Case:

    Ideal for projects using older build tools or requiring compatibility with legacy environments. Also suitable for quick integration using <script> in HTML files.

  • How to Use:

    • Browser (via script tag):
      <script src="./dist/formoose.umd.js"></script>
      <script>
          const formoose = new Formoose();
      </script>
    • Node.js (CommonJS):
      const Formoose = require('./dist/formoose.umd.js');
      
      const formoose = new Formoose();

Choose the Right Build

BuildUse CaseModule SystemExample Integration
formoose.es.jsModern JavaScript projectsES Modules (ESM)import syntax
formoose.umd.jsLegacy environments or direct script useUMD (CommonJS/AMD)<script> or require statement

Both builds are minified for performance, ensuring fast loading and minimal overhead in production environments. Choose the one that best fits your project's requirements.

Usage

  1. Add the data-formoose-form attribute to your form:
<form data-formoose-form>
    ...
</form>
  1. Initialize Formoose in your JavaScript:
document.addEventListener("DOMContentLoaded", () => {
    const formoose = new Formoose();
});

Optionally you can provide a form to Formoose or it will find the forms by data-formoose-form attribute

document.addEventListener("DOMContentLoaded", () => {
    const formoose = new Formoose(document.getElementById("formId"));
});
  1. Add rules to input elements
<form data-formoose-form>
    <input type="text" name="name" data-formoose-required>
    <input type="email" name="email" data-formoose-required data-formoose-email>
    <input type="password" name="password" data-formoose-required data-formoose-min="8">
    <button type="submit">Sign Up</button>
</form>

optionally you can customize a validation message for each validation rule by adding the attribute data-formoose-{rule}-message where {rule} is a rule name

<input type="email" name="email" data-formoose-required data-formoose-required-message="The email is required">

Handling Events

Formoose emits custom events during the form submission lifecycle:

  • formoose:submitting — Before form submission.
  • formoose:submitted — After form submission.
  • formoose:success - After validation succeeds.
  • formoose:error - After validation fails.
document.addEventListener("formoose:submitted", (event) => {
    console.log("Form submitted:", event.detail);
});

Validating Group Elements (Radio, Checkbox)

Wrap <fieldset> element around radio and checkbox elements

<fieldset>
    Man: <input type="radio" name="sex" value="Man">
    Woman: <input type="radio" name="sex" value="Woman">
    Other: <input type="radio" name="sex" value="Other">
</fieldset>

Styling

Formoose does not impose any styles, you are free to style error elements as per your website color theme, Formoose adds the following classes for flexibility:

ElementClassDescription
Inputformoose-invalidThe class will add to the invalid input e.g: (the <input> element)
Divformoose-invalid-feedbackThe class will add to the error container, the parent div of the error message
Pformoose-error-messageThe class will add to the element contains error message e.g: (the p element)

Extend Formoose

Formoose allows you to define custom validation rules to handle specific use cases that aren't covered by the built-in rules. Adding a custom rule is simple and follows these steps:

Define Your Rule

const formoose = new Formoose();

formoose.addRule("max", (value, input) => {
    return value.length <= parseInt(input.getAttribute("data-formoose-max"));
}, (input) => `The value must be less than or equal to ${input.getAttribute("data-formoose-max")} characters`);

Use the Rule in Your Form

<input type="text" name="username" data-formoose-max="16">

Validation Rules

RuleAttributeDescription
Requireddata-formoose-requiredEnsure the field is not empty
Emaildata-formoose-emailValidates an email address
Min Lengthdata-formoose-min="3"Ensure the value is atleast 3 characters
Max Lengthdata-formoose-max="10"Ensure the value is no more than 10 characters
Numberdata-formoose-numberEnsure the value is number
Strong Passworddata-formoose-strongPaswwordEnsures the value contains at least one lowercase letter, one uppercase, one number, and one special character
Same Asdata-formoose-same="password"Ensure the value is same as another input name password
Datedata-formoose-datevalidates a date
Alphadata-formoose-alphaEnsure the value contains only letters e.g. a-z, A-Z
Alpha Numericdata-formoose-alphaNumericEnsure the value contains only letters and numbers e.g. a-z, A-Z, 0-9
Alpha Dashdata-formoose-alphaDashEnsure the value contains letters, numbers, underscores and dashes
Accepteddata-formoose-acceptedEnsure the value is "on", "yes" or "1"
Arraydata-formoose-arrayEnsure the value is array
Date Beforedata-formoose-dateBefore="1990-01-01"Ensure the date value is older than the date provided in the rule
Date Afterdata-formoose-dateAfter="1990-01-01"Ensure the date value is newer than the date provided in the rule
Betweendata-formoose-between="18,65"Validates the value between the given comma separated numbers
Booleandata-formoose-booleanEnsure the value is "true", "1", "false", or "0"
Differentdata-formoose-different="email"Ensure the value is different than another input name email
URLdata-formoose-urlEnsure the value is a valid URL
Indata-formoose-in="option1,option2,option3"Ensure the value exists in the specified comma separated options
Not Indata-formoose-notIn="option1,option2,option3"Ensure the value does not exists in the specified comma separated options
Starts Withdata-formoose-startsWith="test"Ensure the value starts with a specified string
Ends Withdata-formoose-endsWith="test"Ensure the value ends with a specified string
Containsdata-formoose-contains="test"Ensure the value contains a specified string
Not Containsdata-formoose-notContains="test"Ensure the value does not contains a specified string
Phonedata-formoose-phoneEnsure the value is a valid phone
Timedata-formoose-timeEnsure the value is a valid time e.g. 12:00 am
Time 24data-formoose-time24Ensure the value is a valid 24-hour time e.g. 23:59
URLdata-formoose-urlEnsure the value is a valid URL e.g. https://www.example.com
IPv4data-formoose-ipv4Ensure the value is a valid IPv4 e.g. 0.0.0.0
IPv6data-formoose-ipv6Ensure the value is a valid IPv6 e.g. 1000:0ac3:22a2:0000:0000:4b3c:0504:1234
Imagedata-formoose-imageEnsure the value is a valid Image
Formatsdata-formoose-formats="jpg,png"Ensure the file has a valid file format
2.0.0

5 months ago

1.8.1

5 months ago

1.8.0

6 months ago

1.7.0

6 months ago

1.4.0

6 months ago

1.2.2

6 months ago

1.2.1

6 months ago

1.2.0

6 months ago

1.0.2

6 months ago

1.0.0

6 months ago