Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to validate mobile number length in ReactJS ?
Next article icon

How to validate Passport Number is ReactJS?

Last Updated : 12 Jan, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Passport Number validation is an important step in order to authenticate the user's passport number. The following example shows how to validate the user's passport number using the npm module in ReactJS. 

Syntax: 

isPassportNumber(str, countryCode)

Parameters: This function accepts two parameters as mentioned above and described below:

  • str: It is the input value of the string type.
  • countryCode: It is the country code for that passport, for example: 'AM', 'US', 'IN', etc.

Creating React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: After creating the ReactJS application, Install the validator module using the following command:

npm install validator

Project Structure: It will look like the following.

Project Structure

App.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

JavaScript
import React, { useState } from "react"; import validator from 'validator'  const App = () => {    const [errorMessage, setErrorMessage] = useState('')    const validate = (textInput) => {      if (validator.isPassportNumber(textInput,'IN')) {       setErrorMessage('Is Valid Passport Number')     } else {       setErrorMessage('Is Invalid Passport Number')     }   }    return (     <div style={{       marginLeft: '200px',     }}>       <pre>         <h2>Validate Passport Number in ReactJS</h2>         <span>Enter Passport Number: </span><input type="text"           onChange={(e) => validate(e.target.value)}></input> <br />         <span style={{           fontWeight: 'bold',           color: 'red',         }}>{errorMessage}</span>       </pre>     </div>   ); }  export default App 

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:

  • The following will be the output if the user enters an invalid passport number.
  • The following will be the output if the user enters a valid passport number.

Next Article
How to validate mobile number length in ReactJS ?
author
gouravhammad
Improve
Article Tags :
  • Technical Scripter
  • JavaScript
  • Web Technologies
  • ReactJS

Similar Reads

  • How to Validate Octal number in ReactJS?
    The Octal numeral system is the base-8 number system which uses the digits 0 to 7. It is also known as Oct for short. The following example shows how to validate the user entered data and check whether it is valid or not using the npm module in the ReactJS application. ApproachTo validate octal numb
    2 min read
  • How to validate mobile number length in ReactJS ?
    Validating mobile number length in React JS App is an important step to check whether the number entered by the user is genuine or not. It is effective in many case like creating a user form, collection of employee details, etc. Approaches to validate mobile number length in React Js are Table of Co
    3 min read
  • How to validate URL in ReactJS?
    URL (Uniform Resource Locator) is a reference/address to a resource on the Internet. For example, www.geeksforgeeks.com is a URL. The following example shows how to validate the user entered data and check whether it is valid or not using the npm module in the ReactJS application.  Creating React Ap
    2 min read
  • How to Validate a Date in ReactJS?
    Validating input Date in react ensures the correct date input. The valid date input is commonly used in case of calculating days, getting DOB for user data, and other operations etc. Prerequisites:React JSNode JS and NPMApproachTo validate a date in React we will use the validator npm package. Take
    2 min read
  • How to Validate an Email in ReactJS ?
    Validating email in React is an important step to authenticate user email. It ensures the properly formatted email input from the user. The following example shows how to validate the user entered email and checking whether it is valid or not using the npm module in React Application. ApproachTo val
    2 min read
  • How to apply validation on Props in ReactJS ?
    Need of Validating Props in React JS Props are used to pass the read-only attributes to React components. For the proper functioning of components and to avoid future bugs and glitches it is necessary that props are passed correctly. Hence, it is required to use props validation to improve the react
    3 min read
  • How to Validate Number String in JavaScript ?
    Validating a number string in JavaScript involves ensuring that a given string represents a valid number. This typically includes checking for digits, optional signs (+/-), decimal points, and possibly exponent notation (e.g., "1.23e4"). We will use various methods to validate number strings in Java
    2 min read
  • How to perform form validation in React?
    Form validation in React involves ensuring that the data entered into a form meets certain criteria before submission. In this, we will see the form validation in React. Pre-requisitesNodeJS and NPMReactJSReact useState hookHTML, CSS, and JavaScriptSteps to Create React Application And Installing Mo
    4 min read
  • How to Validate JSON Schema ?
    Validating JSON schema ensures that the structure and data types within a JSON document adhere to a predefined schema. This process is crucial for ensuring data consistency and integrity in applications where JSON data is exchanged. ApproachImport the Ajv Library, a popular JSON Schema validator for
    2 min read
  • Create a Password Validator using ReactJS
    Password must be strong so that hackers can not hack them easily. The following example shows how to check the password strength of the user input password in ReactJS. We will use the validator module to achieve this functionality. We will call the isStrongPassword function and pass the conditions a
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences