SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: using a loop as an alternative
Threaded View
-
Jan 7, 2001, 16:07 #1
- Join Date
- Mar 2000
- Location
- Muskegon, MI
- Posts
- 2,328
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I found a tutorial on using the 'confirm()' method of JavaScript. I made some modifications to use it a form validation. Currently, it's a series of repeated if statements. I know it could be simplified with a loop, but I'm not sure on how to do it.
Code:<script language=javascript type="text/javascript"> <!-- function verify(){ if (document.forms[0].firstName.value == "") { alert("You must enter a first name!"); document.forms[0].firstName.focus(); return false; } if (document.forms[0].lastName.value == "") { alert("You must enter a last name!"); document.forms[0].lastName.focus(); return false; } if (document.forms[0].address.value == "") { alert("You must enter an address!"); document.forms[0].address.focus(); return false; } if (document.forms[0].city.value == "") { alert("You must enter a city!"); document.forms[0].city.focus(); return false; } if (document.forms[0].state.value == "") { alert("You must enter a state!"); document.forms[0].state.focus(); return false; } if (document.forms[0].zip.value == "") { alert("You must enter a zip code!"); document.forms[0].zip.focus(); return false; }else{ msg = "Is This Information Correct? \n\n First Name: " + document.forms[0].firstName.value + "\n Last Name: " + document.forms[0].lastName.value + "\n Address: " + document.forms[0].address.value + "\n City: " + document.forms[0].city.value + "\n State: " + document.forms[0].state.value + "\n Zip Code: " + document.forms[0].zip.value + "\n\n"; return confirm(msg); } } // --> </script> <form action="http://localhost/testWeb/" method="post" onSubmit="return verify()"> <table border=0> <tr> <td>First Name: </td> <td><input type=text name="firstName" size=12></input></td> </tr> <tr> <td>Last Name:</td> <td><input type=text name="lastName" size=12></input></td> </tr> <tr> <td>Address:</td> <td><input type=text name="address" size=15></input></td> </tr> <tr> <td>City:</td> <td><input type=text name="city" size=15></input></td> </tr> <tr> <td>State:</td> <td><input type=text name="state" size=2></input></td> </tr> <tr> <td>Zip Code:</td> <td><input type=text name="zip" size=5></input></td> </tr> <tr> <td colspan=4 align="center"><input type=submit value="Submit"></input> <input type=reset value="Reset"></input></td> </tr> </table> </form>
Feel free to copy and paste this code for your own use
Bookmarks