Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Textbox validation with array
Old 03-17-2006, 01:31 AM Textbox validation with array
Justin-Credible's Avatar
Novice Talker

Posts: 12
Trades: 0
I've searched a couple forums and been googling all over the web but I can't find out what I'm doing wrong here. I have a huge form and I'm trying to make sure the textboxes aren't blank. If they are I'm placing a red arrow by the respective box.

I've cut the code down to the following, alerting the name of the field rather than putting in a red arrow (already have that part working). The document.test.FieldName[i].value=="" is causing problems I assume. I wanted to loop through an array rather than have an IF statement for each textbox because I have 40 textboxes.


HTML Code:
<html>
<head>

  <SCRIPT LANGUAGE="JavaScript">
  <!--

  function validate(){
      	var FieldName=new Array(2);
         FieldName[0]="text1";
         FieldName[1]="text2";

         var i=0
         for (i=0; i<2; i++){
           if ( document.test.FieldName[i].value=="" ){
             alert('This isnt filled in: ' + FieldName[i]);
           }
        }
   return true;
   }

  // -->
  </SCRIPT>

</head>

<body>

<form name="test">
 <input type="text" name="text1">
 <input type="text" name="text2">
</form>
<a href="#" onclick="validate()">Click Me</a>
</body>
</html>
Justin-Credible is offline
Reply With Quote
View Public Profile Visit Justin-Credible's homepage!
 
 
Register now for full access!
Old 03-17-2006, 04:45 AM Re: Textbox validation with array
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
  function validate(){
      	var FieldName=new Array(2);
         FieldName[0]="text1";
         FieldName[1]="text2";

         for (i=0; i<FieldName.length; i++){
//		 var field = FieldName[i]
           if ( document.forms['test'].elements[FieldName[i]].value=="" ){
             alert('This isnt filled in: ' + FieldName[i]);
           }
        }
   return true;
   }
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-25-2006, 05:39 PM Re: Textbox validation with array
Justin-Credible's Avatar
Novice Talker

Posts: 12
Trades: 0
Thanks! Works great.
Justin-Credible is offline
Reply With Quote
View Public Profile Visit Justin-Credible's homepage!
 
Reply     « Reply to Textbox validation with array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22779 seconds with 12 queries