-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from KashishJuneja101003/main
Real-time Email Validation for Signup Form
- Loading branch information
Showing
5 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "your-app-name", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node signup_server.js" | ||
}, | ||
"dependencies": { | ||
"express": "^4.17.1", | ||
"body-parser": "^1.19.0", | ||
"cors": "^2.8.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,3 +97,10 @@ form { | |
padding: 0; | ||
} | ||
|
||
.error-message { | ||
color: red; | ||
font-size: 14px; | ||
margin-top: 5px; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
document.getElementById('signupForm').addEventListener('submit', function (e) { | ||
e.preventDefault(); | ||
|
||
const email = document.getElementById('email').value; | ||
const password = document.getElementById('password').value; | ||
const repeatPassword = document.getElementById('repeatPassword').value; | ||
|
||
// Reset previous error messages | ||
document.getElementById('emailError').innerText = ''; | ||
document.getElementById('passwordError').innerText = ''; | ||
document.getElementById('repeatPasswordError').innerText = ''; | ||
|
||
// Validate if fields are empty | ||
if (!email || !password || !repeatPassword) { | ||
alert('Please fill in all fields.'); | ||
return; // Stop further execution if any field is empty | ||
} | ||
|
||
let isValid = true; | ||
|
||
// Email validation | ||
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/; | ||
if (!emailPattern.test(email)) { | ||
document.getElementById('emailError').innerText = 'Please enter a valid email address.'; | ||
isValid = false; | ||
} | ||
|
||
// Password validation | ||
if (password.length < 6) { | ||
document.getElementById('passwordError').innerText = 'Password must be at least 6 characters.'; | ||
isValid = false; | ||
} | ||
|
||
// Repeat Password validation | ||
if (password !== repeatPassword) { | ||
document.getElementById('repeatPasswordError').innerText = 'Passwords do not match.'; | ||
isValid = false; | ||
} | ||
|
||
// If all validations pass, submit the form using Fetch API | ||
if (isValid) { | ||
// Create URL-encoded string | ||
const formData = new URLSearchParams(); | ||
formData.append('email', email); | ||
formData.append('password', password); | ||
formData.append('repeatPassword', repeatPassword); | ||
|
||
fetch('http://localhost:3000/signup', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', // Explicitly set the content type | ||
}, | ||
body: formData.toString(), // Serialize the form data | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
// Show alert based on response from the server | ||
if (data.message === 'Account created successfully!') { | ||
alert('Registration successful!'); | ||
} else if (data.message === 'Email is already registered.') { | ||
alert('This email is already registered.'); | ||
} else { | ||
alert('Error: ' + data.message); | ||
} | ||
document.getElementById('signupForm').reset(); | ||
}) | ||
.catch(error => { | ||
alert('An error occurred. Please try again.'); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import express from 'express'; | ||
import cors from 'cors'; | ||
import bodyParser from 'body-parser'; | ||
|
||
const app = express(); | ||
|
||
// Enable CORS | ||
app.use(cors()); | ||
|
||
// Middleware to parse URL-encoded data (FormData) | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
// Middleware to parse JSON data | ||
app.use(bodyParser.json()); | ||
|
||
const users = []; // Dummy user storage | ||
|
||
app.post('/signup', (req, res) => { | ||
console.log('\nReceived data:', req.body); | ||
const { email, password, repeatPassword } = req.body; | ||
|
||
// Check if all fields are present | ||
if (!email || !password || !repeatPassword) { | ||
return res.status(400).json({ message: 'Please fill in all fields.' }); | ||
} | ||
|
||
// Validate email format | ||
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/; | ||
if (!emailPattern.test(email)) { | ||
return res.status(400).json({ message: 'Please enter a valid email address.' }); | ||
} | ||
|
||
// Password length check | ||
if (password.length < 6) { | ||
return res.status(400).json({ message: 'Password must be at least 6 characters.' }); | ||
} | ||
|
||
// Password match check | ||
if (password !== repeatPassword) { | ||
return res.status(400).json({ message: 'Passwords do not match.' }); | ||
} | ||
|
||
// Check if email already exists | ||
if (users.some(user => user.email === email)) { | ||
console.log('\n'+email+' is already registered!'); | ||
return res.status(400).json({ message: 'Email is already registered.' }); | ||
} | ||
|
||
// Add user to the users array | ||
users.push({ email, password }); | ||
|
||
// Respond with success message | ||
res.status(201).json({ message: 'Account created successfully!' }); | ||
console.log('\n'+email+' is registered successfully!'); | ||
}); | ||
|
||
app.listen(3000, () => { | ||
console.log('Server is running on http://localhost:3000'); | ||
}); | ||
|