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

react-valid-form-component

v1.4.7

Published

React form validation component.

Downloads

70

Readme

react-valid-form

React form validation component.

npm package Build Status Dependencies Status Package Size

Getting started

Install with NPM:

$ npm install react-valid-form-component

Usage

Live Demo CodeSandbox

With this component, you can validate form fields according to the rules you specify. Simply define your form in the <ValidForm></ValidForm> tags to validate.

Component supports standard form elements;

<input />
<select></select>
<textarea></textarea>
Example

When the form is validated, it is automatically posted. You can manually Submit or Fetch using nosubmit prop. You can follow the details about the form with onSubmit={(form, data, valid)} event.

Auto Submit Example CodeSandbox

// App.js
import React from 'react';
import ValidForm from 'react-valid-form-component'

function App() {
    return (
        <ValidForm action="https://httpbin.org/post" method="post">
            <div>
                <label for="validation">Text Example: </label>
                <input
                    type="text"
                    name="validation"
                    id="validation"
                    /* validation rules */
                    required
                    minLength="3"
                    maxLength="50"
                />
            </div>
            <div>
                <label for="validation">Email Example: </label>
                <input
                    type="email" /* validation with type */
                    name="validation"
                    id="validation"
                />
            </div>
            <button type="submit">Send Form</button>
        </ValidForm>
    )
}
export default App;
Manual Fetch Example

Once the form is validated, you can send the data manually.

Fetch Example CodeSandbox

// App.js
import React from 'react';
import ValidForm from 'react-valid-form-component'

function App() {
    const onSubmit = (form, data, valid) => {
        const requestOptions = {
            method: "POST",
            headers: {
                Accept: "application/json",
                "Content-Type": "application/json"
            },
            body: JSON.stringify(data)
        };

        fetch("https://httpbin.org/post", requestOptions)
            .then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    throw new Error(`Response Error, Status Code : ${response.status}`);
                }
            })
            .then(json => {
                console.log(json);
            })
            .catch(function (error) {
                console.error(error);
            });
    }
    return (
        <ValidForm nosubmit onSubmit={(form, data, valid) => onSubmit(form, data, valid)}>
            <div>
                <label for="validation">Text Example: </label>
                <input
                    type="text"
                    name="validation"
                    id="validation"
                    /* validation rules */
                    required
                    minLength="3"
                    maxLength="50"
                />
            </div>
            <div>
                <label for="validation">Email Example: </label>
                <input
                    type="email" /* validation with type */
                    name="validation"
                    id="validation"
                />
            </div>
            <button type="submit">Send Form</button>
        </ValidForm>
    )
}
export default App;
Props

nosubmit Disable auto submit.

novalid "onSubmit" event is also triggered when the form is not valid.

data Default form elements value.

Events

onSubmit={(form, data, valid)} When the form is submitted, it is triggered.

  • form : Html form elemet.

  • data : Form fields data.

  • valid : Form is valid? (true/false)

Default Validation Rules

You can add rules and change warning texts. You can use rules by defining them as type="" or prop. Follow the document for details.

  • required="true" : Required field.

  • number="true" : Only number field. Can be used as Type.

  • alphanumeric="true" : Only alphanumeric character.

  • letters="true" : Only letters.

  • min="integer" : Min value limitations.

  • max="integer" : Max value limitations.

  • minlength="integer" : Min value length limitations.

  • maxlength="integer" : Max value length limitations.

  • email="true" : Only email field. Can be used as Type.

  • url="true" : Only url field. Can be used as Type.

  • confirm="Confirmation Field ID" : Verifies that the two fields have the same value. Such as the "Password Repeat" field.

  • regexp="Regular Expression"

Add Validation Rule

Import Rules and Warnings objects for add rule.

import ValidForm, { Rules, Warnings } from 'react-valid-form';

// rule
Rules.customRule = (value) => {
  return (value === "Custom Rule")
};

// warning alert
Warnings.customRule = (params) => `This field is custom rule ${params}.`

// using
<input type="text" name="validation" id="validation" customRule />
React Select

To use required validation with react-select component.

<Select
  name="reactSelect"
  inputId="reactSelect"
  className="react-select-valid"
  options={[
    { label: 'Option 1', value: '1' },
    { label: 'Option 2', value: '2' },
    { label: 'Option 3', value: '3' },
  ]}
/>
  • Add react-select-valid to the component as className for validation.

  • Fill in the component's inputID and name property.

  • If you don't want to validate, set the inputId property to no-validation.


Author

Barış Ateş