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

onchangeinput

v2.0.3

Published

Onchange Input

Downloads

9

Readme

OnChangeInput Hook

onchangeinput is a custom React hook designed to handle form input changes and validation efficiently. It provides a simple interface to manage form state, validate inputs, and display error messages.

Demo

https://onchangeinput-demo.vercel.app/

Git repository

  https://github.com/Pankajkumar34/onchangeinput_Demo

  
## Installation

To install `onchangeinput`, use npm:

```bash
  npm install onchangeinput

Examples

 const validationRules = {
  email: {
    pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
    errorMessage: {
      patternMsg:"Invalid email format"
    }
  },
  fname: {
    pattern: /^[a-zA-Z]+$/,
    errorMessage: {
      patternMsg:"Only letters are allowed"
    },

  },
  lname: {
    pattern: /^[a-zA-Z]+$/,
    errorMessage: {
      patternMsg:"Only letters are allowed"
    },

  },
  phoneNumber:{
    pattern:/^[0-9]/,
    errorMessage: {
      patternMsg:"Only numbers are allowed"
    },
  },
  password: {
    pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/,
    minLength: 4,
    maxLength: 10,
    errorMessage: {
      minLengthMsg: "Password must be at least 4 characters long",
      maxLengthMsg: "Password must not exceed 10 characters"
    }
  },
  confirmPassword: {
    errorMessage: {
      mismatch: "Passwords do not match"
    }
  },
};
  // Function to get empty fields from the form

const getEmptyFields = (values) => {
const emptyFields = [];
const keyValues = ["fname", "email","lname","password","phoneNumber","confirmPassword"];
for (const field of keyValues) {
  if (!values[field]) {
    emptyFields.push(field);
  }
}
return emptyFields;
};
import React from "react"
import OnChangeInput from 'onchangeinput';

function App() {
  // Initialize the custom hook to manage form state and validation //

  const { values, errors, handleChange, resetForm, setErrors } = OnChangeInput(
    {
      fname: "",
      email: "",
      lname:"",
      phoneNumber:"",
      password:"",
      confirmPassword:""
    },
    validationRules
  );

  // Function to handle form submission
  const submitData = (e) => {
    e.preventDefault();
    const emptyFields = getEmptyFields(values);
    if (emptyFields.length > 0) {
      // If there are empty fields, set errors for each empty field
      emptyFields.forEach(field => {
        setErrors((prev) => ({ ...prev, [field]: `${field} is required` }));
      });
    } else {
      console.log("All values filled:", values);
      // Add further logic for data submission or API calls here
    }
  };

  return (
    <div className="App">
      <input
        type="text"
        name="fname"
        value={values.fname}
        onChange={handleChange}
        placeholder="Enter your first name"
      />

      {errors.fname && <span className="error">{errors.fname}</span>}

      <input
        type="text"
        name="email"
        value={values.email}
        onChange={handleChange}
        placeholder="Enter your email address"
      />
      {errors.email && <span className="error">{errors.email}</span>}
        <input
        type="text"
        name="lname"
        value={values.lname}
        onChange={handleChange}
        placeholder="Enter your lname address"
      />
      {errors.lname && <span className="error">{errors.lname}</span>}
        <input
        type="text"
        name="phoneNumber"
        value={values.phoneNumber}
        onChange={handleChange}
        placeholder="Enter your number address"
      />
      {errors.phoneNumber && <span className="error">{errors.phoneNumber}</span>}
        <input
        type="text"
        name="password"
        value={values.password}
        onChange={handleChange}
        placeholder="Enter your password address"
      />
         <input
        type="text"
        name="confirmPassword"
        value={values.confirmPassword}
        onChange={handleChange}
        placeholder="Enter your confirmPassword address"
      />

{errors.confirmPassword && <span className="error">{errors.confirmPassword}</span>}
      <button onClick={submitData}>Submit</button>
    </div>
  );
}
export default App;

Installation

Install onchangeinput use npm

  npm install onchangeinput

Authors

🚀 About Me

I'm a Mern stack developer...

Other Common Github Profile Sections

👩‍💻 I'm currently working on...

👯‍♀️ I'm looking to collaborate on...

💬 Ask me about...