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
https://forums.phpfreaks.com/topic/205255-multiple-input-validation/
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...');
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.