Jump to content

Multiple Input Validation


justineaguas

Recommended Posts

Hello, I want to know how to validate multiple textboxes in one form. I just studied JavaScript yesterday and I tried this code from www.w3schools.com that validates one textbox in a form.

 

 

function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}
function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(txtProductName, "Fill it out!")==false)
  {return false;}
  }

 

I tried adding another if statement after the code to validate other textboxes other than txtProductName. Sadly, it didn't work. Can anyone help me in validating multiple textboxes in one form only and it only shows one alert message. Please please please. Thanks for the future help!

Link to comment
Share on other sites

// set error to false
var error = false;

// get all the textareas...
var textAreas = document.getElementsByTagName('textarea');

// loop through the textareas, checking the value is not empty...
for ( i=0; i <= textAreas.length; i++ )
{
    // if any one of them is empty, error will be set to true...
    if ( textAreas[i].value == '' )
    {
        error = true;
    }
}

// is there an error? (is error true)
if ( error )
{
    alert('there are errors...');
}

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.