Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to loop through a small form and check for blank fields. I thought it would be simple, but I'm not able to get anything to work. This is my Javascript so far:

var obj = {

fname : document.getElementsByName('fname'),
lname : document.getElementsByName('lname'),
phone : document.getElementsByName('phone'),
email : document.getElementsByName('email'),
span : document.createElement('span'),
txt : document.createTextNode('*Required Field'),



init : function(){
     document.getElementsByName('submit').onclick = obj.validate;
},

validate : function(){
     var check = document.getElementsByTagName('input');
     var len = check.length;
     for(var i=0;i<len;i++)
     {
     if (check[i].value ==='')
     {
          alert('required');
          return false;
     }; 

 };

  },

};

This is the HTML:

<body>
<form method="post" action="">
<div>
<ul>
<li><label>First Name</label><input type="text" name="fname" size="30"  /></li>
<li><label>Last Name</label><input type="text" name="lname" size="30"  /></li>
<li><label>Phone</label><input type="text" name="phone" size="30"  /></li>
<li><label>Email</label><input type="text" name="email" size="30" /></li>
<li><input type="submit" name="submit" value="Submit" /></li>
</ul>
</div>
</form>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript">obj.init();</script>  
</body>
</html> 

I thought i should be able to loop through the inputs and alert if the value was blank, but, it's not working for me. Any help would be awesome.

share|improve this question
1  
obj.check.length should be check.length. check.value should be check[i].value. You should really rename the check variable to something more descriptive, like inputElements. –  UkuleleFury Apr 10 at 0:08
    
@UkuleleFury woops. forgot to delete that obj. I was trying to have check be an object before putting in the function as a variable. But, i changed both of those and it's still not working. Thanks for the advice, though. –  rexer Apr 10 at 0:16
    
I edited my code to what i have now. It is still not working :/ –  rexer Apr 10 at 0:20
    
@rexer What goes wrong exactly? Do you get any errors? –  aw04 Apr 10 at 0:24
1  
@rexer: see my amended answer, below. getElementsByName() returns an array, so init() would not work as-is. It would make more sense to use getElementById() on an ID. –  cybersam Apr 10 at 1:15

3 Answers 3

up vote 2 down vote accepted

Try this (after adding id="submit" attribute to your submit element):

  init : function(){
     document.getElementById('submit').onclick = obj.validate;
  },

  validate : function(){
     var check = document.getElementsByTagName('input');
     var len = check.length;
     for(var i=0;i<len;i++) {
       if (check[i].value ==='')
       {
          alert('required');
          return false;
       }; 
     };
  }
share|improve this answer
    
still getting nothing. This looks like it should work though. Thanks for trying to help me out. –  rexer Apr 10 at 0:19
1  
See my amended answer. You were using an array improperly. You could use getElementById() on an ID instead. –  cybersam Apr 10 at 1:01

Change

if (check.value ==='')

To

if (check[i].value ==='')
share|improve this answer
    
still not working. that was actually one of the things i tried earlier. Glad to see someone else thought it should work.haha. –  rexer Apr 10 at 0:17

You're checking the object check and not each element in it. Do this

if(check[i].value === '')
share|improve this answer
    
I switched it to that, but it is still not working. Thanks for trying to help me, though. –  rexer Apr 10 at 0:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.