Hello Good afternoon Guys
Iam KNR iam having a problem with javascript validation
i want to check validation of all controls at a time means
if i have a form with all fields blank then it has to show message
and if any of the field is filled then it dont have to show the message and
the message about blank field has to shown

if(trim(cname.value) == '')
   {
      alert('Enter a Name');
      cname.focus();
      return false;
   }

please help me

This demo will alert the user, depending on which is field is empty. Lets say for example, ( if field 1 is empty then, field 1 will be identified as invalid entry and as well as with the other field's also).

Don't worry--If you need a simple demo, im sure some other posters will provide another format of this code.
Hope it helps and good day to you...

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>JavaScript Demo</title>
<style id="internal" type="text/css">
/* <![CDATA[ */
label { display : block; margin : 0em 0em 1em 0em; }
/* ]]> */
</style>
<script type="text/javascript">
// <![CDATA[
var validate, showError;
var i, x, invalidEntry, msg, error = [];

showError = function( error ) {
msg = "There were some problems...\n";
   for ( i = 0; i < error.length; i++ ) {
   invalidEntry = i + 1;
   msg += "\n" + invalidEntry + ". " + error[i];
   }  alert( msg ); 
      window.location.reload();       return;
};

validate = function( form ) {
form = ( document.getElementById ) ? document.getElementById("testform") : document.all.testform;
   if ( form ) {
      for ( var x = 1; x < form.elements.length; x++ ) {
         if ( eval("form.txt" + x + ".value.length") <= 0 ) {
         error[error.length] = "Field " + x + " requires a valid entry!";  
         }
      }
      if ( error.length > 0 ) {
      showError( error );
      return false;
      }
   } alert("Thank you for visiting my site!"); return false;
};

// ]]>
</script>
</head>
<body>
<div id="content">
<form id="testform" action="#" onsubmit="return validate( this );">
<div>
<label for="txt1">Field 1: <input type="text" id="txt1" name="txt1" value="" size="20" /></label>
<label for="txt2">Field 2: <input type="text" id="txt2" name="txt2" value="" size="20" /></label>
<label for="txt3">Field 3: <input type="text" id="txt3" name="txt3" value="" size="20" /></label>
<input type="submit" id="sbm" name="sbm" value="submit" />
</div>
</form>
</div>
</body>
</html>
This article has been dead for over six months. Start a new discussion instead.