Form validation using Javascript

By: David Flanagan Emailed: 1765 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The <form> tag supports an onsubmit event handler, which is triggered when the user tries to submit a form. You can use this event handler to perform validation: checking that all required fields have been filled in, for example. If the onsubmit handler returns false, the form is not submitted. For example:

<form name="address" onsubmit="checkAddress()">

<!-- form elements go here -->

</form>

<script>

// A simple form validation function

function checkAddress() {

  var f = document.address; // The form to check

  // Loop through all elements

  for(var i = 0; i < f.elements.length; i++) {

    // Ignore all but text input elements

    if (f.elements[i].type != "text") continue;

    // Get the user's entry

    var text = f.elements[i].value;

    // If it is not filled in, alert the user

    if (text == null || text.length == 0) {

      alert("Please fill in all form fields.");

      return false;

    }

  }

}

</script>

Javascript Home | All Javascript Tutorials | Latest Javascript Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by David Flanagan
Form validation using Javascript
window.frames[i] in Javascript

More Tutorials in Javascript
Use WinSCP to transfer log files remotely using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Javascript to display client date and time on webpage
Getting Browser's height and width using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
Form validation using Javascript
Show how many characters remaining in a html text box using javascript
Click to remove an element from a select html form using javascript
History and evolution of Javascript
Javascript Basics
List of Keywords and reverved words in Javascript
Using toString() in JavaScript to convert data types to String
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Typecasting in Javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
Using ! and !! in Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript
break and continue in Javascript
Most Emailed Articles (in Javascript)
Javascript Basics
Using ! and !! in Javascript
List of Keywords and reverved words in Javascript
break and continue in Javascript
History and evolution of Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
Verifying user input in JavaScript
Use WinSCP to transfer log files remotely using Javascript
Show how many characters remaining in a html text box using javascript
Click to remove an element from a select html form using javascript
Using toString() in JavaScript to convert data types to String
Typecasting in Javascript
The if statement in Javascript