JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 October 3rd, 2012, 05:15 AM
franzlin2010 franzlin2010 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Location: Ghana
Posts: 42 franzlin2010 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 35 m 29 sec
Reputation Power: 4
Send a message via Yahoo to franzlin2010
Smile Help with Text Field Array Validation

Please i have this form with contains multiple text fields with their names as array, pls here is the code.

I want to use javascript to validate the multiple tex fields before they are submitted.
Code:
<form action="index.php" method="POST" id="form" name="form" onsubmit="return check_fields();">

<table width="60%" id="expenses_table" class="again">
<tr>
<td width="60%">Receiver's Name:</td>
<td><input type="text" name="r_name[]" value="" class="receiver_input"></td>
</tr>
<tr>
<td>Description:</td>
<td><input type="text" name="r_desc[]" value="" class="receiver_input"></td>
</tr>                
<tr>
<td>Amount:</td>
<td><input type="text" name="r_amount[]" value="" class="sales_input"></td>
</tr>             
</table><hr class="hrule">


And here is the javacript code but its not working at all pls help me out.
Code:
<script type="text/javascript">
exp_form = document.getElementById("form");
function check_fields(exp_form)
{
	for(i=0;i<=exp_form.elements.length;i++)
	{
		if(exp_form.elements[i].value=="")
		{
			alert('empty fields detected');
			return false;
		}
	}
}
</script>


Thanks a lot in Advance.

Reply With Quote
  #2  
Old October 3rd, 2012, 11:36 AM
YellowBricks YellowBricks is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Hannover, Germany
Posts: 11 YellowBricks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 33 sec
Reputation Power: 0
Post

Hello franzlin,

you seem to have to mistakes in your code.

The first is, that you are trying to pass the variable exp_form to the check_fields() function by giving declaring it as it's parameter, but you don't pass that parameter when calling the function.
Instead move the exp_form variable inside the check_fields() function:
Code:
function check_fields() {
var exp_form = document.getElementById("form");
for(i=0;i<=exp_form.elements.length;i++) 	{ 		
[…]

I also added a var to no clutter the global scope, just a best practice.

Second, you have to make the function accessible when the form renders and not after, e. g. when using a DOM-Ready event callback.

You might want to move away from using inline JS event handlers and rather bind them from within the JS.

I hope I could help you.

Reply With Quote
  #3  
Old October 4th, 2012, 08:03 AM
franzlin2010 franzlin2010 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Location: Ghana
Posts: 42 franzlin2010 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 35 m 29 sec
Reputation Power: 4
Send a message via Yahoo to franzlin2010
thanks a lot for your reply

I have modified as u said, but there is still something wrong with the function i don't know.

When I un-comment the alert line and take out the logical statement at the end of the line. the function works correctly but in turns gives a lot of alert boxes.

I have set a flag in the loop to be raised if empty fields were detected in the forms.

Which i used the if statement to check if the flag was raised outside the loop.

which is still not working pls help me check it out.

thanks a lot

Code:
function check_fields()
{
	var exp_form = document.getElementById("form");
	var error2 = 0;
	for(i=0;i<=exp_form.elements.length;i++)
	{
		if(exp_form.elements[i].value=="")
		{
//			alert('empty fields'+i);
			var e_error_field = 1;	
		}
	}

	if(e_error_field == 1)
	{
		alert('empty fields detected');
		return false
	}
	else
	{
		return true;
	}
}

Reply With Quote
  #4  
Old October 4th, 2012, 10:29 AM
YellowBricks YellowBricks is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Hannover, Germany
Posts: 11 YellowBricks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 33 sec
Reputation Power: 0
The code doesn't work, when you uncomment the alert line, it just delays the reload by waiting for you to click "okay". The problem is, that you are looping through the length of the fields but arrays in JS are zero indexed. Meaning, that when you have, e.g. 4, values in you array, the length is 4, but the maximum index is 3.

So the correct code should be:
Code:
function check_fields() {    
    var exp_form      = document.getElementById("form"),
          error2          = 0,
          e_error_field = 0;
     for(i=0;i<=exp_form.elements.length-1;i++) {
         […]
}


I also moved the e_error_field variable to the top to group all the variables with one var statement.

And by the way, I don't know, if you just didn't include it in your provided markup, but you're missing a </form>

I hope I could help you.

Reply With Quote
  #5  
Old October 4th, 2012, 11:44 AM
franzlin2010 franzlin2010 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Location: Ghana
Posts: 42 franzlin2010 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 35 m 29 sec
Reputation Power: 4
Send a message via Yahoo to franzlin2010
Thank you very much Bro.
Your Solution Worked perfectly
I am still a newbie at Javascript.

I hope you can point me at the right direction to learn.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Help with Text Field Array Validation

Developer Shed Advertisers and Affiliates



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

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap