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-mobile-validator

v1.1.1

Published

A comprehensive mobile number validation library for React applications.

Downloads

25

Readme

React Mobile Validator

react-mobile-validator is a lightweight and user-friendly npm package that simplifies the process of validating mobile numbers based on their respective country codes. This package provides a set of utility functions that can be seamlessly integrated into your React applications to ensure that mobile numbers provided by users are correctly formatted and valid for the selected country.

Validating mobile numbers can be a cumbersome task, especially when dealing with different country codes and formats. With react-mobile-validator, you can effortlessly incorporate mobile number validation into your application's forms and user interfaces, enhancing the overall user experience.

Installation

You can install the package using npm:

npm install react-mobile-validator

or using yarn:

yarn add react-mobile-validator

Usage

To get started with react-mobile-validator, follow these simple steps:

  1. Import the necessary functions:
import validateMobileNumber from "react-mobile-validator";
  1. Validate a mobile number:
const isValidNumber = validateMobileNumber(countryCode, newNumber);
//e.g countryCode = "IN";  newNumber = "885XXXXXXX";

console.log(isValid); // true or false

The validateMobileNumber function takes two arguments: the mobile number as a string and the ISO 3166-1 alpha-2 country code (e.g., 'US' for the United States, 'IN' for India).

Example

Here's a basic example of integrating react-mobile-validator into a React component:

import React, { useState } from "react";
import validateMobileNumber from "react-mobile-validator";
import countryCodeList from "./countryCodes.json";
function App() {
  const [mobileNumber, setMobileNumber] = useState("");
  const [countryCode, setCountryCode] = useState("");
  const [isValid, setIsValid] = useState(false);
  /******************* 
  @Purpose : handleInputChange
  @Author : react-mobile-validator
  ******************/
  const handleInputChange = (event) => {
    const newNumber = event.target.value;
    setMobileNumber(newNumber);
    const isValidNumber = validateMobileNumber(countryCode, newNumber);
    if (isValidNumber) {
      setIsValid(true);
    } else {
      setIsValid(false);
    }
  };
  return (
    <div>
      <label htmlFor="country">Choose a country:</label>
      <select
        id="country"
        name="countryName"
        onChange={(e) => setCountryCode(e.target.value)}
      >
        {Object.values(countryCodeList)?.map((country) => {
          return (
            <option key={country.iso2} value={country.iso2}>
              {country.name}
            </option>
          );
        })}
      </select>
      <label htmlFor="mobile">Enter MobileNumber:</label>
      <input
        type="text"
        id="mobile"
        name="mobileName"
        value={mobileNumber}
        onChange={handleInputChange}
      />
      <p>{isValid ? "Valid" : "Invalid"} mobile number</p>
    </div>
  );
}

export default App;

Conclusion

With react-mobile-validator, you can seamlessly integrate mobile number validation into your React applications, ensuring that users provide correctly formatted mobile numbers for their respective countries. This package simplifies the validation process and enhances the overall user experience of your application's forms and input interfaces.

Feel free to explore the package's functions and customize them according to your application's needs. For more information, refer to the GitHub repository of the project.

If you encounter any issues or have suggestions for improvements, please don't hesitate to contribute by submitting issues or pull requests to the repository. Happy coding! 🚀