Jump to content

exclude fields being checked for empty when using getElementByTagName


laPistola

Recommended Posts

Hello

 

Im trying to build a way of checking for empty fields on large form where you dont have to enter every input name it needs to check

 

This is my code:

 

function checkF() {
var allInputs = document.getElementsByTagName('input');
for (var i=0; i<allInputs.length; i++) {
	var fValue = allInputs.item(i).innerHTML;
	if (fValue=="") { // here i need to exclude say field1 , field 3 and field 10 from being checked
		return false;
		alert("Required field(s) are empty, please make sure all required fields are entered.")
	} else {
		return true;
	}
}
}

 

what i would like is where i made the comment in the above code a way of excluding curtain fields by there name being checked if there empty, not sure its possible but a way using the .item() must be.

 

Any help would be great thank you

Link to comment
Share on other sites

I would suggest that the proper method would be to explicitly state what fields should be validated instead of using a blacklist. But if you must...

 

I would create an array of the fields that do not need to be validated. Then just check the field name against the array to determine if you should validate or not. I found the array prototype below on this page: http://snippets.dzone.com/posts/show/3631 So, I take no responsibility as to it's capability.

 

Also, you cannot validate and input by using innerHTML. You have to use the value property. Plus, if you do a "return" before your alert, the alert will never fire.

 

Give this a try (not tested)

//Prototyp[e function for searching an array
Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}

//Create an array of non required fields
var nonRequiredFields = new Array ('field-1', 'field-3', 'field-10');

function checkF()
{
    //reference to input fields object
    var allInputs = document.getElementsByTagName('input');

    //Iterrate through all the input fields
    for (var i=0; i<allInputs.length; i++)
    {
        //Check that field is NOT in non required fields
        if (!nonRequiredFields.find(allInputs[i].name))
        {
            //Check if field is empty
            if(!allInputs[i].value)
            {
                alert("Required field(s) are empty, please make sure all required fields are entered.")
                return false;
            }
        }
    }
    //All required fields are enbtered
    return true;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.