user1400803
user1400803

Reputation:

JavaScript: Function not running

I'm currently learning JavaScript and trying to validate a simple form.

I'm using the revealing module pattern and using a for loop to check if input fields are empty on form submit.

The problem is that the validate function isn't firing and I see no errors in the console.

Also, If my JS can be improved please do let me know.

JSFiddle

http://jsfiddle.net/n4TvL/

HTML

<form action="" method="post" name="frm" id="form">
    <input id="fullname" placeholder="Name" name="input">
    <input id="email" placeholder="Email" name="input">
    <input type="submit" id="submit">
</form>

JS

"use strict";

var signUp = (function() {

    var formSubmit = function() {
      var inputField = document.getElementsByTagName('input');
      document.forms['frm'].onsubmit = function(e) {
          alert('ello');
          val();
          e.preventDefault();
      }
    };

    function val() {
        var check = document.getElementsByName('input');
        var len = check.length;
        for(var i = 0; i < len; i++) {
            if(check[i].length === "") {
                alert('empty');
            }
        }
    };

    return {
      init: formSubmit
    }

})();

signUp.init();

Upvotes: 0

Views: 73

Answers (2)

Petro
Petro

Reputation: 3652

Actually your val() function is being called, your problem is with

EDIT: .length should .innerHTML.length < 1

See: http://jsfiddle.net/n4TvL/4/

         if(check[i].innerHTML.length < 1) {

            alert('empty');
        }

Upvotes: -1

Buck Doyle
Buck Doyle

Reputation: 6387

val is firing, which you can see if you put an alert in at the beginning.

Instead of

check[i].length

you should have

check[i].value

Upvotes: 3

Related Questions