Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a simple form and I need to write a PHP script that tests whether an e-ma

ID: 3690776 • Letter: I

Question

I have a simple form and I need to write a PHP script that tests whether an e-mail address is input correctly. Verify that the input begins with series of characters, followed by the @ character, another series of characters, a period (.), and a final series of characters. Test your program, using both valid and invalid e-mail addresses.

Here is the form:

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>


<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="form.php">

E-mail: <input type="text" name="email">
<span class="error">*</span>
<br><br>

<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Explanation / Answer

Please use the below php script to test if an email address is valid or in-valid:

<?php
/*
* PHP Test Script - How to check if a email address is valid using regular expressions
*/

//A valid email address
$email = "email@example.com";
//the pattern is "any letter or number followed by @ followed by any letter or number
//followed by . followed by 2-4 letters and maybe another . (for tlds like co.uk)
$okay = preg_match(
'/^[A-z0-9_-]+[@][A-z0-9_-]+([.][A-z0-9_-]+)+[A-z.]{2,4}$/', $email
);
if ($okay) {
echo $email." is valid<br />";
} else {
echo $email." is invalid<br />";
}

//An invalid email address
$email = "email[at]example[dot]com";
//the pattern is "any letter or number followed by @ followed by any letter or number
//followed by . followed by 2-4 letters and maybe another . (for tlds like co.uk)
$okay = preg_match(
'/^[A-z0-9_-]+[@][A-z0-9_-]+([.][A-z0-9_-]+)+[A-z]{2,4}$/', $email
);
if ($okay) {
echo $email." is valid<br />";
} else {
echo $email." is invalid<br />";
}
?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote