Page 186 - DCAP408_WEB_PROGRAMMING
P. 186
Web Programming
Notes <form action=”submit.htm” onsubmit=”return validate_form(this)”
method=”post”>
Email: <input type=”text” name=”email” size=”30">
<input type=”submit” value=”Submit”>
</form>
</body>
</html>
Output
After clicking submit button on empty e-mail the message is as shown in Figure 6.4.
Figure 6.6
E-mail Validation
The function below checks if the content has the general syntax of an e-mail.
This means that the input data must contain at least an @ sign and a dot (.). Also, the @ must not be the
first character of the e-mail address, and the last dot must at least be one character after the @ sign:
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf(“@”);
dotpos=value.lastIndexOf(“.”);
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false;}
else {return true;}
}
}
In the below Example the entire script, with the HTML form could look something like this.
Example: Illustrating E-mail validation.
<html>
<head>
<script type=”text/javascript”>
function validate_email(field,alerttxt)
{
180 LOVELY PROFESSIONAL UNIVERSITY