Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JS concatenate 1

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
US
Hi all,
I'm trying to validate an array checkboxes utilizing this For loop,
Code:
for (x=0;x<=document.edit.lCount.value;x++)
	{
		if ((document.edit.idLoc_(x).value != "") || (document.edit.channelNo_(x).value == ""))
		{	
			alert("Invalid Location or Channel has been selected!")
			document.edit.idLoc_(x).focus();
			return false;
		}
	}
from this array checkbox...
Code:
<input type="checkbox" name="idLoc_<%=x%>" value="<%=arrLoc(0,x)%>" <%if instr(vLoc,arrLoc(0,x)) then response.write "checked"%>>&nbsp;<b><%=arrLoc(1,x)%>
I just cannot understand what happen to the js code that I can't seem to concatenate the x value correctly for the checkbox values to be validated.

Please help!
 
In case you wonder why is the channelNo checkbox to validate, here it is:
Code:
<%
						'display array of channel system in checkboxes
						dim icount
						icount = 0
						for i=1 to arrLoc(2,x)
							imatch = ", " & i & ","
							if icount = 8 then
								response.write "</tr><tr>"
								icount = 0
							end if
			%>				
							<td align="left"><input type="checkbox" name="channelNo_<%=x%>" value="<% =i %>" <%if instr(channelNo,imatch) then response.write "checked"%>><% =i %></td>
			<%
							icount = icount + 1 
						Next
			%>
 
You should use the forms and elements collections - it will solve all your problems. It's more typing, but it's a good habit to get into. Even better, assign ids to your elements and use getElementById. Anyway, this should work:
Code:
for (x=0;x<=document.[!]forms["edit"].elements["lCount"][/!].value;x++)
    {
        if ((document.[!]forms["edit"].elements["idLoc_" + x][/!].value != "") || (document.[!]forms["edit"].elements["channelNo_" + x][/!].value == ""))
        {    
            alert("Invalid Location or Channel has been selected!")
            document.[!]forms["edit"].elements["idLoc_" + x][/!].focus();
            return false;
        }
    }

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
Kaht, you are the man!

Thank you very much! I will definitely spend more time learning JS form and elements collection. :)
 
One more question...

How come this particular validate code doesn't return me to the error like the other validation I have, instead, it kept going forward after I hit OK at the error popup?

Code:
for (x=0;x<=document.forms["edit"].elements["lCount"].value;x++)
	{
		if ((document.forms["edit"].elements["idLoc_" + x].value != "") || (document.forms["edit"].elements["channelNo_" + x].value == ""))
		{	
			alert("Channel has not been selected! Please make your selection and Submit again.")
			document.forms["edit"].elements["channelNo_" + x].focus();
			return false;
		}
		else if ((document.forms["edit"].elements["idLoc_" + x].value == "") || (document.forms["edit"].elements["channelNo_" + x].value != ""))
		{	
			alert("Location for selected channel(s) has not been checked! Please review your selection and Submit again.")
			document.forms["edit"].elements["idLoc_" + x].focus();
			return false;
		}
	}
	
	return true
 
it kept going forward after I hit OK at the error popup

What do you mean "kept going forward"? The page submitted? The loop continued to process more error checks?

By the way, for what it's worth, you should start a new thread when you have a new and unrelated question. Often times you'll get quicker help that way. Many helpers are apt to jump on a thread with 0 replies - but if they see a thread has 4 replies and a star awarded, often times they won't even look at the thread because it appears to have already been resolved. Starting a new thread for a new question gives you maximum exposure to your question.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
>for (x=0;x<=document.forms["edit"].elements["lCount"].value;x++)
You sure x attains the .value or it is bounded above but not attaining it.
[tt]for (var x=0;x[highlight]<[/highlight]document.forms["edit"].elements["lCount"].value;x++)[/tt]
If the function encountered error, it returns immediately to the caller, ie, the submit follows.
 
Thank you, Kaht, for the suggestion.

About your question related to "going forward", I meant the form kept submitted, saved and redirected... as if no error has been found.

To tsuji, I'm not sure I understand your question completely, but I can say that the x value, so far, returns the same .value as I expected.
 
I found out something weird here!!!

After I removed a couple lines .focus() when alert found from the validation, the code seems to work fine now.

Why?
 
Your answer may be here from tsuji's posting:
If the function encountered error, it returns immediately to the caller, ie, the submit follows.

Is it possible that the element you were trying to give focus to did not exist? If that is the case, the function would error and the submission process would continue.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
Nevermind everyone.

I'll start this post on the new one because the problem seems to be a little more complicated. Please help me on the next post. Thanks!
 
You can try this just to see if those lines were throwing an error:
Code:
for (x=0;x<=document.forms["edit"].elements["lCount"].value;x++)
    {
        if ((document.forms["edit"].elements["idLoc_" + x].value != "") || (document.forms["edit"].elements["channelNo_" + x].value == ""))
        {    
            alert("Channel has not been selected! Please make your selection and Submit again.")
[!]try {[/!]
            document.forms["edit"].elements["channelNo_" + x].focus();
[!]}
catch (e) {
   alert(e);
}[/!]
            return false;
        }
        else if ((document.forms["edit"].elements["idLoc_" + x].value == "") || (document.forms["edit"].elements["channelNo_" + x].value != ""))
        {    
            alert("Location for selected channel(s) has not been checked! Please review your selection and Submit again.")
[!]try {[/!]
            document.forms["edit"].elements["idLoc_" + x].focus();
[!]}
catch (e) {
   alert(e);
}
[/!]
            return false;
        }
    }
    
    return true

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top