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

form-control-validator

v1.0.6

Published

simple and easy integrated form validator

Downloads

24

Readme

Form Validator

A simple and easy integrated form validator for JavaScript. This package provides straightforward functionality to validate form inputs with minimal setup.

Description

form-control-validator is designed to help developers implement form validation quickly and efficiently. With customizable rules, it ensures that your forms meet the required standards before submission.

Features

  • Required Field Validation: Ensures essential fields are not left empty.
  • Minimum Length Validation: Checks if the input meets specified length requirements.
  • Regex Validation: Validates inputs against custom regular expressions.
  • Custom Error Messages: Allows the definition of specific error messages for better user feedback.

Installation

You can install it using NPM or Yarn:

npm install form-control-validator
yarn add form-control-validator

Usage

Once installed, you can import and use the form-control-validator function in your React components:

import React, { useState } from "react";
import { validateForm } from "form-control-validator";

const MyForm = () => {
  const [formData, setFormData] = useState({
    username: {
      value: "",
      rules: {
        required: true,
        minLength: 3,
        maxLength: 15,
        pattern: /^[a-zA-Z0-9]*$/, // Alphanumeric pattern
        messages: {
          required: "Please enter your username.",
          minLength: "Your username must have at least 3 characters.",
          maxLength: "Your username must not exceed 15 characters.",
          pattern: "Username can only contain alphanumeric characters.",
        },
      },
    },
    password: {
      value: "",
      rules: {
        required: true,
        minLength: 8,
        messages: {
          required: "Please enter your password.",
          minLength: "Your password must have at least 8 characters.",
        },
      },
    },
    email: {
      value: "",
      rules: {
        required: true,
        pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, // Simple email regex
        messages: {
          required: "Please enter your email.",
          pattern: "Please enter a valid email address.",
        },
      },
    },
  });

  const [errors, setErrors] = useState({});

  const handleChange = (e) => {
    const { name, value } = e.target;
    setFormData((prev) => ({
      ...prev,
      [name]: {
        ...prev[name],
        value: value,
      },
    }));
  };

  const handleSubmit = (e) => {
    e.preventDefault();
    const validationErrors = validateForm(formData);
    setErrors(validationErrors || {});

    if (!validationErrors) {
      console.log("Form Submitted", {
        username: formData.username.value,
        password: formData.password.value,
        email: formData.email.value,
      });
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label>Username:</label>
        <input
          type="text"
          name="username"
          value={formData.username.value}
          onChange={handleChange}
        />
        {errors.username &&
          errors.username.map((error, index) => (
            <span key={index} style={{ color: "red" }}>
              {error}
            </span>
          ))}
      </div>

      <div>
        <label>Password:</label>
        <input
          type="password"
          name="password"
          value={formData.password.value}
          onChange={handleChange}
        />
        {errors.password &&
          errors.password.map((error, index) => (
            <span key={index} style={{ color: "red" }}>
              {error}
            </span>
          ))}
      </div>

      <div>
        <label>Email:</label>
        <input
          type="email"
          name="email"
          value={formData.email.value}
          onChange={handleChange}
        />
        {errors.email &&
          errors.email.map((error, index) => (
            <span key={index} style={{ color: "red" }}>
              {error}
            </span>
          ))}
      </div>

      <button type="submit">Submit</button>
    </form>
  );
};

export default MyForm;

ADVANCE

If you need validation form in OnChange then take help from this

const handleChange = (e) => {
  const { name, value } = e.target;
  setFormData((prev) => ({
    ...prev,
    [name]: {
      ...prev[name],
      value: value,
    },
  }));

  // Validate field on change
  const fieldValidationErrors = validateForm({ [name]: formData[name] });
  setErrors((prevErrors) => ({
    ...prevErrors,
    [name]: fieldValidationErrors ? fieldValidationErrors[name] : null,
  }));
};