JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 5th, 2008, 10:46 AM
nikko50 nikko50 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 273 nikko50 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 4 m 18 sec
Reputation Power: 5
Vaildate text field

Hi all. I have the below function to validate a text field. On one page this charge account field is duplicated for several rows and is named "charge[]". How can I alter the below function to validate each row if each field is named the same.?

//VALIDATE CHARGE
if(theForm.charge){
if(!theForm.charge.value.match( /(^\d{5}$)|(^\d{6}$)|(^\d{6}-\d{6}$)/) ){
alert("Please input a valid Charge Account!")
theForm.charge.style.backgroundColor='pink';
theForm.charge.focus();
return false
}}

Reply With Quote
  #2  
Old May 5th, 2008, 12:14 PM
vbrtrmn's Avatar
vbrtrmn vbrtrmn is offline
I <3 Javascript
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2004
Location: Northern Virginia
Posts: 1,775 vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 6 Days 21 h 39 sec
Reputation Power: 139
Send a message via ICQ to vbrtrmn Send a message via AIM to vbrtrmn Send a message via MSN to vbrtrmn Send a message via Yahoo to vbrtrmn
You're not a noob, use CODE tags!

You probably shouldn't be naming a bunch of text fields the same, it isn't really a valid way of doing HTML.

Here's a stripped down example, to get you started:
Code:
<form name="theForm">
<input type="text" value="" name="name" />
<input type="text" value="" name="address" />
<input type="text" value="" name="phone" />
<input type="text" value="" name="charge[0]" />
<input type="text" value="" name="charge[1]" />
<input type="text" value="" name="charge[2]" />
<input type="text" value="" name="charge[3]" />
<input type="button" value="test" onclick="validateCharges()" />
</form>
<script type="text/javascript">
function validateCharges() {
    var inputs = document.theForm.getElementsByTagName('input');
	var re = new RegExp('charge\\[\\d+\\]');
	for (var i=0;i<inputs.length;i++) {
		if (!re.exec(inputs[i].name)) continue;
		if (!inputs[i].value.match(/(^\d{5}$)|(^\d{6}$)|(^\d{6}-\d{6}$)/)) {
			inputs[i].style.backgroundColor='pink';
		}
	}
}
</script>
Comments on this post
Joseph Taylor agrees!
__________________
------------- vbrtrmn --------------
i think i'm missing some vowels here
------------------------------------
---------- js.antinoc.net ----------
------------------------------------
--- The Two Types of Programmers ---

Reply With Quote
  #3  
Old May 6th, 2008, 07:29 AM
nikko50 nikko50 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 273 nikko50 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 4 m 18 sec
Reputation Power: 5
Hi. Thanks for the help. I named all the fields "charge[]" so that I can populate that array and process on the server side. I'm not to good with javascripting and am confused how to validate each charge[] field. I did also give each field an id for another server side purpose. Each field also has an id as "id='charge$i'". $i being the row loop number beginning with "0". Maybe I can validate by the ID number. If anyone can help I would greatly appreciate it.
Tracy

Reply With Quote
  #4  
Old May 6th, 2008, 07:58 AM
Joseph Taylor's Avatar
Joseph Taylor Joseph Taylor is offline
Text Ninja
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2005
Location: Vancouver, British Columbia, Canada
Posts: 596 Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 1 h 12 m 43 sec
Reputation Power: 107
Send a message via Skype to Joseph Taylor Send a message via XFire to Joseph Taylor
What vbrtrmn posted does work. Apparently that code doesn't quite click with you though, so I'm offering this:

Code:
<form name="theForm">
<input type="text" value="" name="name" />
<input type="text" value="" name="address" />
<input type="text" value="" name="phone" />
<input type="text" value="" name="charge[]" />
<input type="text" value="" name="charge[]" />
<input type="text" value="" name="charge[]" />
<input type="text" value="" name="charge[]" />
<input type="button" value="test" onclick="validateCharges()" />
</form>
<script type="text/javascript">
function validateCharges() {
    var inputs = document.getElementsByName('charge[]');
    var invalid = false;
    for (var i=0;i<inputs.length;i++) {
        if (!inputs[i].value.match(/(^\d{5}$)|(^\d{6}$)|(^\d{6}-\d{6}$)/)) {
            inputs[i].style.backgroundColor='pink';
            invalid = true;
        }
    }
    return !invalid; // for when this is in an onsubmit handler
}
</script>


Same deal here, but it won't work if any elements outside of the form being validated are named charge[]. I'm guessing that's not going to be an issue.

Although XHTML elements can legally share the same name, I'm of vbrtrmn's mind on this one; it's pretty unusual to have multiple text fields share the same name. But if this is handled consistently by all browsers then great, do you what you want, and more power to you.

Reply With Quote
  #5  
Old May 6th, 2008, 08:12 AM
nikko50 nikko50 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 273 nikko50 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 4 m 18 sec
Reputation Power: 5
Yes they both work fantastic. Thanks so much you guys!!!
Tracy))

Reply With Quote
  #6  
Old May 6th, 2008, 10:38 AM
Hammer65's Avatar
Hammer65 Hammer65 is offline
Web Developer/Musician
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2004
Location: Lincoln Nebraska
Posts: 1,927 Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 19 h 8 m 50 sec
Reputation Power: 500
Send a message via AIM to Hammer65
Nothing wrong with form fields having the same name, that is standard practice with radio buttons. It is a logical grouping of controls. The problem here is, that Javascript doesn't recognize the square bracket syntax that the server side language does. When you have multiple fields of the same name, they are simply put into a named node map. You access each node by using the item() method of the map.

Javascript Code:
Original - Javascript Code
  1.  
  2. var map = document.forms['myform'].elements['charge[]'];
  3. for(var i = 0;i < map.length;i++)
  4. {
  5.    alert(map.item(i).value);
  6. }
Comments on this post
Joseph Taylor agrees: Yeah, that way actually is better.
__________________
"Strange women lying in ponds distributing swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony! Well, but you can't expect to wield supreme executive power just 'cause some watery tart threw a sword at you! I mean, if I went 'round saying I was an emperor just because some moistened bint had lobbed a scimitar at me, they'd put me away!"

Last edited by Hammer65 : May 6th, 2008 at 11:33 AM. Reason: Mistyped highlight tags

Reply With Quote
  #7  
Old May 6th, 2008, 12:31 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Click here for more information.
 
Join Date: Jul 2004
Location: USA
Posts: 15,149 Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Weeks 2 Days 2 h 47 m 18 sec
Reputation Power: 1294
item() is unnecessary in JavaScript because you can access NodeLists with array syntax. If I remember correctly, IE has issues with item() too.
__________________
Spreading knowledge, one newbie at a time.

Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

IE7: the generation 7 browser new in a world of generation 8 browsers.
Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

Reply With Quote
  #8  
Old May 6th, 2008, 01:29 PM
Hammer65's Avatar
Hammer65 Hammer65 is offline
Web Developer/Musician
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2004
Location: Lincoln Nebraska
Posts: 1,927 Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 19 h 8 m 50 sec
Reputation Power: 500
Send a message via AIM to Hammer65
Quote:
Originally Posted by Kravvitz
item() is unnecessary in JavaScript because you can access NodeLists with array syntax. If I remember correctly, IE has issues with item() too.


I have actually had the opposite experience with IE, but whatever works. I believe, that the "proper" documented way, is to use item().

Reply With Quote
  #9  
Old May 6th, 2008, 01:36 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Click here for more information.
 
Join Date: Jul 2004
Location: USA
Posts: 15,149 Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Weeks 2 Days 2 h 47 m 18 sec
Reputation Power: 1294
They're the same. I prefer using the older, shorter syntax.

http://www.w3.org/TR/DOM-Level-3-Co...pt-binding.html
Quote:
Originally Posted by DOM3-Core
Functions of objects that implement the NodeList interface:

item(index)
This function returns an object that implements the Node interface.
The index parameter is a Number.
Note: This object can also be dereferenced using square bracket notation (e.g. obj[1]). Dereferencing with an integer index is equivalent to invoking the item function with that index.

Reply With Quote
  #10  
Old May 6th, 2008, 01:43 PM
Hammer65's Avatar
Hammer65 Hammer65 is offline
Web Developer/Musician
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2004
Location: Lincoln Nebraska
Posts: 1,927 Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 19 h 8 m 50 sec
Reputation Power: 500
Send a message via AIM to Hammer65
Ok. I stand corrected

Quote:
The item() method is meant for other programming languages where nodeLists like those returned by getElementsByTagName are not conveniently accessible as if they were arrays.

You don't need item() at all in JavaScript.


The DOM is supposed to be language independent, to this end there are a few cases like this where methods are exposed that are not necessary to use. This is one of those cases, one that I wasn't aware of until now.

Last edited by Kravvitz : May 6th, 2008 at 01:55 PM. Reason: fixed the link

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Vaildate text field


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!