Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Validation via Arrays

Validating a form (easier step) by GUJUm0deL
Posted: 7 Feb 02

  This idea came to me as a hunch.  At my job I have to do alot validation of forms and I get tired to write the entire document.form.name.value and other forms of checking (like selected, etc...) so I thought "Hmmmm, can U use arrays??" and lo-and-behold here it is...
  This works in IE and NS6 (not NS4, for some reason...it doesn't like the for loop)...you can use textboxes, select choices and radiobuttons, etc...all you have to do is make sure that all the form elements have the same id attribute name...
  This is an example code...enjoy!!  :)

<title>Validation via Arrays</title>

function GUJUm0deL() {
//input the msg message here...just increment by one for more alerts...
var msg1 = "please answer 1";
var msg2 = "please answer 2";
var msg3 = "please answer 3";
var msg4 = "please answer 4";
var msg5 = "please answer 5";

//make sure the id=ONE is the same in all checks (id=ONE is defined in the <form> next to each form element...
  for(i=0; i<=document.form1.ONE.length; i++) {
    if(document.form1.ONE[0].value == "") {
      return false;
    if(document.form1.ONE[1].value == "") {
      return false;
    if(document.form1.ONE[2].value == "") {
      return false;
    if(document.form1.ONE[3].value == "") {
      return false;
    if(document.form1.ONE[4].options[0].selected == true) {
      return false;


<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="mailto:me@aol.com" onSubmit="return GUJUm0deL()">
    <input type="text" id="ONE" name="textfield">
    <input type="text" id="ONE" name="textfield2">
    <input type="text" id="ONE" name="textfield3">
    <input type="text" id="ONE" name="textfield4">

    <select id="ONE" name="select">
      <option selected>Select One</option>
    <input type="submit" name="Submit" value="Submit">

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close