iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 13, 2008
MySpace Profile Page Resources
HTML Goodies
 
May 13, 2008
How to Upload Your Photos onto the Web
HTML Goodies
 
May 13, 2008
Email Marketing for MySpace Artists
HTML Goodies
 
May 13, 2008
Top Online Marketing Techniques
HTML Goodies
 
May 13, 2008
I want to create a site just like ____, is that a violation of...
About
 
May 12, 2008
Film Makers, Bands and Comedians Welcome on MySpace
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /JavaScript

Client-side text field validation with Javascript 

  Views:    24237
  Votes:    11
by Steve Adcock 4/30/04 Rating: 

Synopsis:

Javascript is as powerful as any client-side language comes, and text field validation is a tremendously desired capability many webmasters want for their forms. Let's take a look at a little javascript for the purpose of validating feedback forms.
Pages: firstback1 3 forwardlast
The Article

Amazing Javascript error checking

Each form element belongs, or lives, within one single form, so when error checking multiple forms on one single page, no discrepancies will surface. When checking a particular form field, we call it by the name and check its value. So, let's say we have the following form item in our first document form:

<input type="text" Name="FirstName">

We don't have to define a value within the HTML coding because we rely on your web site viewer to do that. To make sure that when the form is submitted, the form item is checked before processing, we'll use javascript's onSubmit function within the form tag, like so:

<form action="test.php" method="post" onSubmit="return checkme()">

All tags rest in the <head> and </head> portion of our HTML document. In addition, a Javascript function is used and named whatever you like, to error check the text box, which may be a new concept to many fairly light Javascript programmers. A function is simply a set, or chunk, of code used with or without variables to perform a particular function. Here's what I've used to check the text box named FirstName. The elements in green signify Javascript comments and will not be displayed when the page is loaded.

<SCRIPT LANGUAGE="javascript">

<!-- // Hide the following code from non-Javascript enabled browsers

// The following sets the cursor automatically in the FirstName text box field

function focus() // Define function focus

{
// Sets cursor to FirstName input element
    document.forms[0].FirstName.focus();
}

function checkme() // Define function checkme
{
    if (document.forms[0].FirstName.value == "")
    {
    alert("You did not enter your first name. Please provide it.");
    document.forms[0].FirstName.focus();return(false)
    }
}
//-->

</SCRIPT> // End the Javascript script

 

Beyond the focus() function, we first use an if statement to check whether the input element FirstName is blank. If it is, an error will be displayed in the form of a popup window and the form will not be processed. You can simply copy and paste for any form element in your form, changing the field name, of course.

Pages: firstback1 3 forwardlast

Similar/related articles:


 
  Sponsors