internet.com
Library
Magazines

webreference.com

Java Boutique

Search Engine Watch

PC Webopedia

The Web Developer's Virtual Library

   Library

Sample page for JavaScript Tutorial

Press the submit button to verify form data.

Your name:
Your email:
Your phone number:

Here's the source code to the above example: <HEAD> <TITLE>JavaScript Examples: Verifying email Address Format</TITLE> <SCRIPT language="JavaScript"> <!-- to hide script contents from old browsers function VerifyData() { // Source code to check form data goes here. // Create a variable to keep track of whether the form is valid. // Initializing this value to 1 means, in effect, that // the form is valid unless the value changes sometime in the // routine. var valid = 1 if (document.Customer.FirstLast.value == "") { valid = 0 } if (!CheckEmail(document.Customer.Email.value)) { valid = 0 } if (!CheckPhoneNumber(document.Customer.Phone.value)) { valid = 0 } // Here we decide whether to submit the form. if (!valid) { alert("Please complete all the form fields with valid values.") } return valid } function CheckEmail(TheAddress) { var valid = 1 var atPos = 0 // Note: Remove the comments from the following line to see // the indexOf method in action. //alert("@ character is located at " + TheAddress.indexOf("@")) // First make sure @ is at least at position 1. // Remember the first position is 0 atPos = TheAddress.indexOf("@") if (atPos < 1) { // Return false if @ isn't found. valid = 0 } else { // If @ is found, make sure it isn't last character in string. if (atPos == TheAddress.length-1) { valid = 0 } } return valid } function CheckPhoneNumber(TheNumber) { var valid = 1 var GoodChars = "0123456789()-+ " var i = 0 if (TheNumber=="") { // Return false if number is empty valid = 0 } for (i =0; i <= TheNumber.length -1; i++) { if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) { // Note: Remove the comments from the following line to see this // for loop in action. // alert(TheNumber.charAt(i) + " is no good.") valid = 0 } // End if statement } // End for loop return valid } // end hiding contents from old browsers --> </SCRIPT> <LINK REL="SHORTCUT ICON" href="http://webdeveloper.com/favicon.ico"> <SCRIPT LANGUAGE=JavaScript> <!-- OAS_url = 'http://mjxads.internet.com/RealMedia/ads/'; OAS_sitepage = 'intm/webdev/www.webdeveloper.com/javascript'; OAS_listpos = 'house_ribbon,125x125-1,125x300,125x800,336x280,468x60-1,468x60-2,accessunit,cp1,cp10,cp11,cp12,cp13,cp14,cp2,cp3,cp4,cp5,cp6,cp7,cp8,cp9,sitetext-1,stickypost'; OAS_query = ''; OAS_target = '_top'; OAS_version = 10; OAS_rn = '001234567890'; OAS_rns = '1234567890'; OAS_rn = new String (Math.random()); OAS_rns = OAS_rn.substring (2, 11); function OAS_NORMAL(pos) { var_a=('<A HREF="' + OAS_url + 'click_nx.cgi/' + OAS_sitepage ); var_b=(var_a + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' ); var_c=(var_b+ OAS_query + '" TARGET=' + OAS_target + '>'); document.write(var_c); var_aa=('<IMG SRC="' + OAS_url + 'adstream_nx.cgi/' + OAS_sitepage ); var_bb=( var_aa + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?'); var_cc=( var_bb + OAS_query + '" BORDER=0></A>'); document.write(var_cc); } //--> </SCRIPT> <SCRIPT LANGUAGE=JavaScript1.1> <!-- OAS_version = 11; if (navigator.userAgent.indexOf('Mozilla/3') != -1 ) OAS_version = 10; if (navigator.userAgent.indexOf('Mozilla/4.0 WebTV') != -1) OAS_version = 10; if (OAS_version >= 11) { var_aaa=('<SCRIPT LANGUAGE=JavaScript1.1 SRC="' + OAS_url); var_bbb=(var_aaa + 'adstream_mjx.ads/' + OAS_sitepage + '/1'); var_ccc=(var_bbb + OAS_rns + '@' + OAS_listpos + '?' + OAS_query ); var_ddd=(var_ccc + '"><\/SCRIPT>'); document.write(var_ddd); } //--> </SCRIPT> <SCRIPT LANGUAGE=JavaScript> <!-- document.write(''); function OAS_AD(pos) { if (OAS_version >= 11) OAS_RICH(pos); else OAS_NORMAL(pos); } //--> </SCRIPT></head> <BODY> <H2>Sample page for JavaScript Tutorial</H2> Press the submit button to verify form data. <P> <FORM name="Customer" action="" method="POST" onSubmit="return VerifyData()"> Your name: <INPUT type=text name="FirstLast" size=30 maxlength=75 value=""> <BR> Your email: <INPUT type=text name="Email" size=30 maxlength=75 value=""> <BR> Your phone number: <INPUT type=text name="Phone" size=30 maxlength=75 value=""> <BR> <INPUT type="submit"> <INPUT type="reset"> </FORM> </BODY>
Web Developer® Site Feedback
Web Developer®
Copyright © 2000 internet.com Corporation. All rights reserved.

Web Developer® Home Over a dozen topics in detail Live Chat Downloads Book and Product Reviews Threaded Discussions How-To/Articles/Links Developer Daily News Subscribe Search Corporate Information Advertise Events Publications internet.com Home