BIT351 · 0 · 5 marks
Write a program to validate email id and phone number using React.
Write a program to validate email id and phone number using React. [5]
React Program to Validate Email ID and Phone Number
Given
This is a programming/implementation question, not a numerical problem. There is no numeric data (no matrices, burst times, page strings, etc.) to extract.
Requirements from the question:
- Framework: React
- Validate: Email ID and Phone Number
- Marks: 5 (implies a complete working component with validation logic)
No numeric inputs are missing because none are required for this task.
STEP 2 - SOLVE: Model Answer
The standard approach uses functional components with the useState hook, controlled inputs, and regular expressions for validation.
import React, { useState } from 'react';
function ValidationForm() {
const [email, setEmail] = useState('');
const [phone, setPhone] = useState('');
const [emailError, setEmailError] = useState('');
const [phoneError, setPhoneError] = useState('');
const [successMsg, setSuccessMsg] = useState('');
// Email validation using RegEx
const validateEmail = (value) => {
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!value) return 'Email is required.';
if (!emailRegex.test(value)) return 'Invalid email format.';
return '';
};
// Phone validation (10-digit number)
const validatePhone = (value) => {
const phoneRegex = /^[0-9]{10}$/;
if (!value) return 'Phone number is required.';
if (!phoneRegex.test(value)) return 'Phone must be exactly 10 digits.';
return '';
};
const handleSubmit = (e) => {
e.preventDefault();
const emailErr = validateEmail(email);
const phoneErr = validatePhone(phone);
setEmailError(emailErr);
setPhoneError(phoneErr);
if (!emailErr && !phoneErr) {
setSuccessMsg('Email and Phone Number are valid!');
} else {
setSuccessMsg('');
}
};
return (
<div style={{ padding: '30px', maxWidth: '400px', margin: 'auto' }}>
<h2>Email and Phone Validation</h2>
<form onSubmit={handleSubmit}>
<div style={{ marginBottom: '15px' }}>
<label>Email ID:</label>
<input
type="text"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter your email"
/>
{emailError && <p style={{ color: 'red' }}>{emailError}</p>}
</div>
<div style={{ marginBottom: '15px' }}>
<label>Phone Number:</label>
<input
type="text"
value={phone}
onChange={(e) => setPhone(e.target.value)}
placeholder="Enter 10-digit phone number"
/>
{phoneError && <p style={{ color: 'red' }}>{phoneError}</p>}
</div>
<button type="submit">Validate</button>
{successMsg && <p style={{ color: 'green' }}>{successMsg}</p>}
</form>
</div>
);
}
export default ValidationForm;
Explanation of Key Parts
| Concept | Purpose |
|---|---|
useState | Stores input values and error/success messages |
Controlled inputs (value + onChange) | Keeps React state synced with input fields |
e.preventDefault() | Stops the browser from reloading on submit |
| Email regex | /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ matches [email protected] |
| Phone regex | /^[0-9]{10}$/ accepts exactly 10 digits |
Email regex breakdown:
[a-zA-Z0-9._%+-]+: local part (letters, digits, common symbols)@: mandatory at-symbol[a-zA-Z0-9.-]+: domain\.[a-zA-Z]{2,}$: dot + top-level domain of 2+ letters
Phone regex breakdown:
^[0-9]{10}$: ensures the string is only 10 numeric characters
Sample Behaviour
- Input
[email protected]and9812345678-> "Email and Phone Number are valid!" (green) - Input
ramgmail.com-> "Invalid email format." (red) - Input phone
98123-> "Phone must be exactly 10 digits." (red)
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.