Hello. I am trying to have someone enter an email address and if it’s invalid, it prompts that it’s an invalid email address. I have my code here and it keeps saying “Uncaught TypeError: Cannot read properties of undefined (reading ‘match’) at ValidateEmail”
'use strict'
///listen to 29:35 in voice notes!!! ///
/* Add a function to get the current date and time.
Write the date on the page in a user friendly format */
// Create function to capitalize first char of firstName variable //
const capFirstChar = (str) => str.charAt(0).toUpperCase() + str.slice(1);
// Get user's name //
const firstName = prompt('What is your name?');
// Switch statement to get time of day //
const currTime = new Date();
currTime.getHours();
const currHour = currTime.getHours();
switch (true) {
case (currHour < 12):
document.getElementById('greeting').innerHTML = `Good morning, ${capFirstChar(firstName)}!`;
console.log("Good morning!");
break;
case (currHour > 12 && currHour < 17):
document.getElementById('greeting').innerHTML = `Good afternoon, ${capFirstChar(firstName)}!`;
console.log("Good afternoon!");
break;
default:
document.getElementById('greeting').innerHTML = `Good evening, ${capFirstChar(firstName)}!`;
console.log("Good evening!");
break;
};
const userEmail = 'email@email.com';
const emailSplit = userEmail.split('@');
console.log(emailSplit);
const var1 = userEmail.split([0]);
const var2 = userEmail.split([1]);
console.log(var1)
console.log(var2)
//Function to validate email //
// function validEmail(email) {
// const emailRegex = /[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}/;
// const valid = email.match(emailRegex);
// return valid;
// };
function ValidateEmail(input) {
var validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
if (input.value.match(validRegex)) {
alert("Valid email address!");
document.form1.text1.focus();
return true;
} else {
alert("Invalid email address!");
document.form1.text1.focus();
return false;
}
}
let testEmail = prompt('What is your email?');
ValidateEmail(testEmail);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="trivia.css">
<title>Trivia Game!</title>
</head>
<body>
<header>
<p id="greeting"></p>
<h1>Trivia Game!</h1>
<!-- <p class="between">(Between 1 and 20)</p> -->
<button class="btn again">Again!</button>
<div class="number">?</div>
</header>
<main>
<section class="left">
<input type="number" class="guess" />
<button class="btn check">Check!</button>
</section>
<section class="right">
<p class="message">Start guessing...</p>
<p class="label-score">💯 Score: <span id="score">0</span></p>
<p class="label-highscore">
🥇 Highscore: <span id="highscore">0</span>
</p>
</section>
</main>
<script src="trivia.js"></script>
<script>quiz();</script>
</body>
</html>