Part 3 validating form data Create a new document called l5p

Part 3; validating form data:

Create a new document called l5p3.html. It should have a form with a text input box for an email address and a Submit button. Submitting the form (either by clicking the button or pressing enter when the button has focus) should call a function that validates the email address. The function should use a regular expression to validate the email address and return true if valid, or use alert() to display an error message and return false if not. If the function returns false, the form should not be submitted. Otherwise, submit the form to http://weblab.kennesaw.edu/formtest.php.

Your regular expression should test for exactly this: One or more word characters Exactly one at-sign One or more word characters Exactly one period Two or more characters that are a-z, A-Z, 0-9, period, or hyphen (One way to get \"two or more\" is to look for one such character and then look for one or more. Don\'t forget about character classes.)

Solution

<script>
function validateemail() {
var x=document.myform.email.value;
var atposition=x.indexOf(\"@\");
var dotposition=x.lastIndexOf(\".\");
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){
alert(\"Please enter a valid e-mail address \ atpostion:\"+atposition+\"\ dotposition:\"+dotposition);
return false;
}
}
</script>
<body>
<form name=\"myform\" method=\"post\" action=\"#\" onsubmit=\"return validateemail();\">
Email: <input type=\"text\" name=\"email\"><br/>   
<input type=\"submit\" value=\"submit\">
</form>

Part 3; validating form data: Create a new document called l5p3.html. It should have a form with a text input box for an email address and a Submit button. Subm

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site