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 🙏

© 2025 – Pkg Stats / Ryan Hefner

global-form-validator

v1.0.3

Published

A multi-country form validation library supporting phone numbers, postal codes, and national IDs.

Downloads

20

Readme

global-form-validator

A multi-country form validation library supporting phone numbers, postal codes, and national IDs. This package helps to validate user input in forms based on country-specific rules for phone numbers, postal codes, and national IDs.


Installation

To use the global-form-validator package in your React project, follow these steps:

1. Install the Package

You can install the package using either npm or yarn:

# Using npm
npm i global-form-validator

Usage

Once you’ve installed the package, you can import the validation functions for phone numbers, postal codes, and national IDs into your React components.

2. Import the Package

Import the necessary validation functions into your component file:

import { phoneValidator, postalCodeValidator, nationalIdValidator} from 'global-form-validator';

Example

Here’s an example React form with validation for phone numbers, postal codes, and national IDs:

import React, { useState } from 'react';
import { phoneValidator, postalCodeValidator, nationalIdValidator} from 'global-form-validator';

const FormComponent = () => {
  const [phone, setPhone] = useState('');
  const [postalCode, setPostalCode] = useState('');
  const [nationalId, setNationalId] = useState('');

  const [phoneValid, setPhoneValid] = useState(false);
  const [postalCodeValid, setPostalCodeValid] = useState(false);
  const [nationalIdValid, setNationalIdValid] = useState(false);

  const handlePhoneChange = (event) => {
    setPhone(event.target.value);
    setPhoneValid(phoneValidator('usa', event.target.value));  // Validate USA phone number
  };

  const handlePostalCodeChange = (event) => {
    setPostalCode(event.target.value);
    setPostalCodeValid(postalCodeValidator('usa', event.target.value));  // Validate USA postal code
  };

  const handleNationalIdChange = (event) => {
    setNationalId(event.target.value);
    setNationalIdValid(nationalIdValidator('iran', event.target.value));  // Validate Iranian national ID
  };

  return (
    <div>
      <h2>Validation Form</h2>

      {/* Phone Number */}
      <div>
        <label>Phone Number (USA):</label>
        <input type="text" value={phone} onChange={handlePhoneChange} />
        <p>{phoneValid ? 'Valid Phone Number' : 'Invalid Phone Number'}</p>
      </div>

      {/* Postal Code */}
      <div>
        <label>Postal Code (USA):</label>
        <input type="text" value={postalCode} onChange={handlePostalCodeChange} />
        <p>{postalCodeValid ? 'Valid Postal Code' : 'Invalid Postal Code'}</p>
      </div>

      {/* National ID */}
      <div>
        <label>National ID (Iran):</label>
        <input type="text" value={nationalId} onChange={handleNationalIdChange} />
        <p>{nationalIdValid ? 'Valid National ID' : 'Invalid National ID'}</p>
      </div>
    </div>
  );
};

export default FormComponent;

Validation Details

Supported Countries

Phone Numbers:

Validate phone numbers for countries such as the USA, Turkey, Iran, Russia, and more.

Postal Codes:

Validate postal codes for countries like the USA, Germany, France, and more.

National IDs:

Validate national IDs for countries such as Iran, Turkey, Russia, and more.

API

phoneValidator(country, phoneNumber);
// Validates the phone number based on the country.

country:

The country code (e.g., 'usa', 'iran', 'turkey').

phoneNumber:

The phone number string to validate.

Example:

phoneValidator('usa', '(123) 456-7890'); // returns true or false
postalCodeValidator(country, postalCode);
// Validates the postal code based on the country.

country:

The country code (e.g., 'usa', 'germany', 'france'). postalCode: The postal code string to validate. Example:

postalCodeValidator('usa', '90210'); // returns true or false
nationalIdValidator(country, nationalId);;
// Validates the national ID based on the country.

country:

The country code (e.g., 'iran', 'turkey', 'russia'). nationalId: The national ID string to validate. Example:

validateNationalId('iran', '1234567890'); // returns true or false

Known Issues and Future Improvements

While intl-form-validator works for many countries and validates phone numbers, postal codes, and national IDs, some issues still need attention:

Incomplete Coverage:

Not all countries are covered yet. We plan to expand the list of supported countries.

Edge Cases:

Some edge cases may not be handled perfectly in certain formats. We encourage developers to contribute and help improve this package. If you find any issues or have suggestions for improvements, please visit our GitHub repository. https://github.com/mohammadhalimi/form-validator

Conclusion

The global-form-validator package helps simplify form validation for phone numbers, postal codes, and national IDs across multiple countries. It’s easy to integrate into your React forms and provides real-time validation feedback.

Feel free to contribute or suggest improvements! Happy coding!

License

This project is licensed under the MIT License