I am trying to create a form where people can add their email addresses to sign up to a mailing list. I am struggling to validate whether they actually entered an e-mail address or not with JavaScript.
Here is the HTML form:
<script type="text/javascript" src="validate-email.js"></script>
<form id="updateform" action='send.php' onsubmit="return validateForm();" method='post'>
<input type="email" name="emailaddress" placeholder="Your e-mail address"/>
<input type="submit" name="submit" value="Submit">
</form>
And here is the JavaScript file's contents:
function validateForm()
{
var x=document.forms["updateform"]["emailaddress"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Please enter a valid e-mail address to receive updates.");
return false;
}
}
What am I doing wrong? I am a complete beginner...
EDIT: Why is this down-voted? What did I do wrong? If you read my comments, this is a legitimate problem, and the solutions I searched on Stack Overflow do not meet my needs.