Thursday, October 23, 2025
HomeLanguagesJavascriptHow to validate Passport Number is ReactJS?

How to validate Passport Number is ReactJS?

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS