I have been tasked to learn basic JavaScript as part of my collage coarse, and i've taken this section if code from the W3C website and commented it out.
Is my interpretation of what this script is doing correct?
Im using this script as an example of how JavaScript can be used to validate information client side.
Thank you for your time. Any additional info that can further my understanding is appreciated.
<html>
<head>
<script type="text/javascript"> //This tells the browser the following is JavaScript.
function validate_email(field,alerttxt) //This defines the function.
{
with (field)
{
apos=value.indexOf("@"); //Tries to find the position of the value given.
dotpos=value.lastIndexOf("."); //Tries to find the last occurrence of the value given.
if (apos<1||dotpos-apos<2) //checks there is a “.” after the “@” character. || is and operator.
{alert(alerttxt);return false; //If there is not open an alert box.
else {return true;} //true and false are boolean values to be used later
}
}
function validate_form(thisform) //New function.
{
with (thisform)
{
if (validate_email(email,"Not a valid e-mail address!")==false) //Check outcome of previous function.
{email.focus();return false;} //not sure what this specifically does. I know its to do with setting the | back in the box, but i think it does it after the alert box is closed?
}
}
</script>
</head>
<body>
<form action="submit.htm" onsubmit="return validate_form(this);" method="post">
calls functions from header and ties it to the submit button (EDP).
Email: <input type="text" name="email" size="30"> creates the input box object.
<input type="submit" value="Submit"> creates the submit button.
</form>
</body>
</html>