Website hositng, design and development Specialists - Prairie 21, Incorporated
Learn How Now
HomeContact UsInformationSearch
with Professional Video Training
Web Design ServicesWeb Design TrainingWeb Development ResourcesFree Web Desgin Downloads
HTML
JavaScript
Links
INDEX
Title
Introduction
What is JavaScript
Intro to Objects
Common Events
Capturing Events
Data Validation
Images
Windows
Summary
Next Steps
JavaScript
Previous

Next

Data Validation

Unlike other scripting languages such as VBScript, JavaScript does not include a substantial list of pre-defined functions for data manipulation such as checking for valid dates or formatting dates, etc....  Therefore we must either write them ourselves or find a reliable source for pre-written functions.

We can then incorporate these functions into our scripts as in the following example (leftTrim is a function that has been written in JavaScript).

function MyForm_onSubmit() {
  var bnSuccess =
true;
  var strErrors = "";

 
if(leftTrim(document.MyForm.FName.value)
      .length==0) {
    strErrors = "\n First Name";
    bnSuccess =
false;
  }
 
if(leftTrim(document.MyForm.LName.value)
      .length==0) {
    strErrors = strErrors + "\n Last Name";
    bnSuccess =
false;
  }
 
if (leftTrim(document.MyForm.CName.value)
      .length==0) {
    strErrors = strErrors + "\n City Name";
    bnSuccess =
false;
  }
 
if (bnSuccess == false) {
    alert("This form has errors on the"+
          "following items"+strErrors+
          "\n\nPlease correct these errors"+
          "before submitting form.");
  }
 
return bnSuccess;
}

In the above example, the variable "bnSuccess" is used to track if an error exits and conditionally display an error message.  The value is then returned to the form, which if the value is false will cause the form submission to be canceled.

The variable "strErrors" is used to accumulate a list of errors which is inserted into the error message letting the user know what is wrong with the form values.  The "\n" is a special line break or new line character that can be inserted into a JavaScript string (a collection of characters or text).  This will work like a carriage return on a typewriter.