SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validation form HELP!!!

    here is my javascript and html code

    Code:
    <script language="javascript">
    
    function check() {
    var a1=document.form1.fname.value;
    var a2=document.form1.lname.value;
    var a3=document.form1.email.value;
    var a4=document.form1.phone_number.value;
    var a5=document.form1.usern.value;
    var a6=document.form1.pass.value;
    var a7=document.form1.ip.value;
    var a8=document.form1.type.value;
    
    
        if(a1&&a2&&a3&&a4&&a5&&a6&&a7) {return true;}
        else { alert("Όλα τα πεδία εκτός από\n την κατηγορία\n είναι απαραίτητα"); return false;}
    
        }
    
    </script>
    
    
    
    
    <form name='form1' method='post' action='' onSubmit='return check()'><td>Ονομα</td><td><input type="text" name='fname' size="20">
    Επίθετο<input type="text" name='lname' size="20">
    E-mail<input type="text" name='email' size="20">
    Τηλέφωνο<input type="text" name='phone_number' size="20">
    IP<input type="text" name='ip' size="20">
    Username<input type="text" name='usern' size="20">
    Password<input type="password" name='pass' size="20">
    Κατηγορίες<select name='category[]' multiple><option value='1'>1<option value='2'>2<option value='3'>3</select>
    Τύποι Ανακοίνωσης
    <select name='type[]' multiple><option value='1'>Α<option value='2'>B<option value='3'>C<option value='4'>D</select>
    <input type='submit' value='Submit' name='submit' >
    </form>
    I cant validate that from. I want all fields to be filled except to name='category[]'
    Last edited by jonsof; Mar 16, 2003 at 09:33.

  2. #2
    Bangarang! Karloff's Avatar
    Join Date
    Mar 2003
    Location
    Manchester, United Kingdom
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there, try this code snippet. I included some comments so you know understand how this works.

    Code:
    <script language="javascript">
    function check() {
     var el = document.forms['form1'].elements; // reference to form elements
     for (var i = 0; i < el.length; i++) {	  // loop through all elements in form
      if (el[i].name != "category[]") {	  // if they arent category[]
       if (el[i].value == "") {		   // check if empty
    	el[i].focus();				 // focus empty input
    	return false;				  // and return false so the form doesnt submit
       }
      }
     }										  // otherwise it submits :)
    }
    </script>
    <form name='form1' method='post' action='about:blank' onSubmit='return check()'>
     <input type="text" name='fname' size="20"><br>
     <input type="text" name='lname' size="20"><br>
     <input type="text" name='email' size="20"><br>
     <input type="text" name='phone_number' size="20"><br>
     <input type="text" name='ip' size="20"><br>
     <input type="text" name='usern' size="20"><br>
     <input type="password" name='pass' size="20"><br>
     <select name='category[]' multiple><option value='1'>1<option value='2'>2<option value='3'>3</select>
     <select name='type[]' multiple><option value='1'>?<option value='2'>B<option value='3'>C<option value='4'>D</select>
     <input type='submit' value='Submit' name='submit' >
    </form>

    Greetings,
    Karl


    I'm desperately trying to figure out why Kamikaze pilots wore helmets. - George Carlin

  3. #3
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Search the damn forum! There's another topic where I gave someone a form validation script I wrote...

  4. #4
    Bangarang! Karloff's Avatar
    Join Date
    Mar 2003
    Location
    Manchester, United Kingdom
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregc
    Search the damn forum! There's another topic where I gave someone a form validation script I wrote...
    Hmm... I can understand how such repeated question to most basic JavaScript problems that have been answered, explained, flipped around, tossed and juggled about 10000x before and counting, created discontent with you.

    Just keep in mind that sometimes beginners fail to see the connection on 'abstract' explanations which aren't _directly_ connected to their problem/case. In such situations, I find it more helpful (as I can remember myself) to see examples and an accompanying explanation of the solution which is directly related to the problem.

    If nothing else, I hope jonsof read through the comments and tried to see why and how this works so that if he goes and changes a few fields in his form, he can change the script, if at all, to suite his new form :-)
    Karl


    I'm desperately trying to figure out why Kamikaze pilots wore helmets. - George Carlin


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
  •