2

i want to check if values of a form is empty or not? i am trying to write my own validator after many validators like jquery.ketchup.0.3.2 failed me (or maybe i failed them ) but i cant make this function to work.

function validate() {
    alert("hmmm");
    var inputs = document.forms["register"].getElementsByTagName("input");
    for (var item in inputs) {
        if(item.value.trim() == "")
            alert("nullField");
    }
}

would you please tell me what is wrong with the above function?

2
  • 6
    or maybe i failed them :) Commented Apr 23, 2013 at 13:43
  • which error do you get? where are you applying this function in the HTML? Can you post a jsfiddle? Commented Apr 23, 2013 at 13:43

4 Answers 4

3

I'm wondering if it's the for loop you used. To loop over a collection (array), use a normal for loop, not a for in. Maybe this fixes it:

function validate() {
    alert("hmmm");
    var inputs = document.forms["register"].getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if(inputs[i].value.trim() == "")
            alert("nullField");
    }
}

Note that .trim() isn't available in all browsers: http://kangax.github.io/es5-compat-table/#String.prototype.trim

A jQuery alternative, probably preferred, is to use something like:

$('form[name="register"]').find('input[type="text"]').each(function () {
    var $this = $(this);
    if (!$.trim($this.val())) {
        // Left empty
    }
});

References:

Sign up to request clarification or add additional context in comments.

2 Comments

thnx - it worked :) just for clarification can i change change other attributes of null fileds by calling inputs[i].someMethod on them?
@dave Yep, exactly. For example, you could change their border with inputs[i].style.border = "1px solid #ff0000";
3

If you're going to use jQuery, then use jQuery.

validate(){
  var $inputs = $('input', 'form[name="register"]');
  $inputs.each(function(){
    var val = $.trim(this.value);
    if(!val.length){
       // Not valid
    }
  });
}

3 Comments

If you're going to suggest using jQuery, then use it to do everything the OP had. You left out the .forms["register"] part
@Ian - the OP suggested using jQuery when they tagged the question jQuery. Have updated with your point though, well caught.
My only point was that you didn't find the form first, that's it.
0

If you want to use only javascript:

1st of all make sure that the trim function is defined:

if(!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g,'');
  };
}

Now for actual function:

 function validate() {
    var inputs = document.forms["register"].getElementsByTagName("input");
    for (var i = 0, len = inputs.length; i < len ; i++) {
        if(inputs[ i ].value.trim() == ""){
            // Validation failed
        }
    }
    // Validation successful
}

Comments

-2

I strongly (and I mean STRONGLY) recommend not writing your own validations. Nevertheless, there are a couple of things that are wrong with your code.

  1. The for..var..in construct iterates through all the properties of the object. So an object foo = { bar: '0', baz: '1'} would iterate through bar and baz (the properties, not the values). If you want to get the value you need to indicate so by doing for(var i in foo){ console.log(foo[i]);}, or in your example: inputs[item].value.
  2. The getElementsByTagName return a NodeList (see here ). NodeLists have additional properties beside the elements in the list.

If you're using jQuery, then you simply do $("#register input").val() to get the array of relevant values and you can use the $.map function to do something for each value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.