SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard westmich's Avatar
    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:&nbsp;</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>
    Also, a loop would allow me to add or subtract fields in the form.

    Feel free to copy and paste this code for your own use
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey westmich,

    Try using this function. I created verifyOne() as a generic use. What you do is you first define the name of the form element; then, you define what you want it "called" in alert() and confirm(). For instance:

    verifyOne("firstName","First Name")

    You see?


    Anyway, try using this:

    Code:
    <script language="javascript" type="text/javascript">
    <!--
    function verifyOne(){
    	msg = "Is This Information Correct? \n\n";
    	for(j=0;j<verifyOne.arguments.length;j+=2){
    		frmElement=eval("document.forms[0]."+verifyOne.arguments[j]);
    		if(frmElement.value==""){
    			alert("You must enter a "+verifyOne.arguments[j+1]);
    			frmElement.focus();
    			return false;
    		}else{
    			msg+=" "+verifyOne.arguments[j+1]+": "+frmElement.value+"\n";
    		}
    	}
    	msg+="\n";
    	return confirm(msg);
    }
    
    function verify(){
    	return verifyOne("firstName","First Name","lastName","Last Name","address","Address","city","City","state","State","zip","Zip");
    }
    // -->
    </script>

    Hope that helps,
    aDog

  3. #3
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did this help you?

  4. #4
    SitePoint Enthusiast spleenboy's Avatar
    Join Date
    Nov 2000
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I think it's a cool script, A-Dog. Very functional. Karma for you!
    s-p-l-e-e-n-d-i-f-e-r-o-u-s

  5. #5
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Arielladog. I haven't had a chance to use it yet.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •