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!

Validation alert but not stop 1

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
US
I'm having this validation which I intent to check whether one of the two (2) checkboxes are not checked accordingly. What I mean is I have an array of checkboxes namely idLoc_(x), and for each of the idLoc_(x), I included another array of checkboxes namely channelNo_(y), now I want to make sure user must check BOTH sides or NEITHER one of the sides for the form to submit. If idLoc checked and ChannelNo is not checked or the other way around, alert will be acquired and correction must be made before the form can be processed. That's all!!!

So far, I got the problem when the form continue to submit after alert was popped up, and here is the code
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.")
			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.")
			return false;
		}
	}
	
	return true

Thanks!
 
Oh, I forgot, I need to include my form fields as well, don't I.

Code:
<%
				'start arrLoc
				dim vLoc
				If isArray(arrLoc) then
					dim x
					For x=0 to ubound(arrLoc,2)
						channelNo = ""
						'get selected channel(s) belong to the customer in the location
						dim c
						for c=0 to ubound(arrChan,2)
							if arrChan(0,c) = cint(id) and arrChan(1,c) = cint(arrLoc(0,x)) then
								vLoc = arrChan(1,c)
								channelNo = ", " & arrChan(2,c) & ","
							end if
						next
%>
					<tr>
						<td colspan="2" valign="middle">
							<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)%></b>
						</td>
					</tr>
					<tr>
						<td align="right">Channels:</td>
						<td>
							<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr>
			<%
						'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
			%>				
							</tr></table>
						</td>
					</tr>
<%
					Next
				'end arrLoc
				End If
%>
 
cumap, if you could include the client-side generated code it would be a lot easier to debug the problem. There is a lot of VBScript mixed in with the code, so it's hard for those of us (myself included) that don't use VBScript to figure out what your html elements are supposed to be.

Do a view-source of the page and paste that here, or better yet just provide a link to the page.

Did you try the try/catch block that I mentioned in your last post to see if the focus method was causing an error?

-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]
 
Thanks kaht,
Unfortunately, I'm developing this page in my localhost so the best I can do is "view-source" only.

I didn't see you reply about the "try/catch", but I also realized that my specification in the IF...ELSE IF scenerio is incorrect. What I want is && for the 2 IF cases, not ||. Because I want (in English)...

if (this checkbox is not checked) AND (its checkboxes is checked) then...
else if (this checkbox is checked) AND (its checkboxes is not checked) then...

It would be more sense than utilizing OR (||) in this case, wouldn't it?

the source-code
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"[/URL] />
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>A1 Webcams DVR Admin</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="/includes/admin.css" />
<script src="/includes/x.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">  
<!--
function adjustLayout() {
	mHeight = xHeight("main");
	newHeight = xClientHeight() - 65
	if(mHeight < newHeight) {
		xHeight("main", newHeight);
	}
	xHeight("navigation", xHeight("main"));
}

window.onload = function() {  
	xAddEventListener(window, "resize", adjustLayout, false);
	adjustLayout();
}
//-->
</script>

<script language="Javascript">
<!--
	function confirmSubmit()
	{
		var agree=confirm("This action can not be undone. Are you sure you want to continue?");
		if (agree)
			return true ;
		else
			return false ;
	}
	
	function CheckAll(formObject) 
	{
		var chk = formObject.checkAll.checked;
		var len = formObject.elements.length;
		for(var i=0; len >i; i++) 
		{
			var elm = formObject.elements[i];
			if (elm.type == "checkbox")
			{
				elm.checked = chk;
			}
		}
	}
	
	function confirmFoundSave()
	{
		var agree=confirm("Match is found. Do you want to continue?");
		if (agree)
			return true ;
		else
			return false ;
	}
-->
</script>

</head>
<body>
	
	<div id="logo"><img src="/images/adminlogo.jpg" /></div>
	<div id="navigation">
	
			<div class="navlink"><B>&raquo;</B>&nbsp;<a href="default.asp">Home</a></div>
			<div class="navlink"><B>&raquo;</B>&nbsp;<a href="admin.asp">Location(s)</a></div>
			<div class="navlink"><B>&raquo;</B>&nbsp;<a href="admin.asp?view=customers">Customers</a></div>
			<br />
			<div class="navlink"><B>&raquo;</B>&nbsp;<a href="admin_logout.asp">Logout</a></div>
	
	</div>
	<div id="main">
	

<script language="javascript">
<!--
function validate()
{
	if (document.edit.password.value == "")
	{
		alert("Please enter Password.")
			document.edit.password.focus();
		return false;
	}
	if (document.edit.custname.value == "")
	{
		alert("Please enter Customer Name.")
			document.edit.custname.focus();
		return false;
	}
	var x
	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.")
			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.")
			return false;
		}
	}
	
	return true
}
-->
</script>


<p class="pagetitle">Update Customer</p>
<p class="p12b">

	<a href="admin.asp?view=customers">List ALL Customers</b></a>
	
	 | <a href="cust_del.asp?action=del&id=8" onClick="return confirmSubmit()">Remove This Customer</a>

</p>
<p class="p12b">

<BR>
<form name="edit" method="post" action="cust_save.asp" onSubmit="return validate();">
	<input type="hidden" name="id" value="8">
	<table border="0" cellpadding="2" cellspacing="0" class="p12b">
		<tr>
			<td align="right" width="20%"><p>&nbsp;</p></td>
			<td 			  width="80%">
				<input type="checkbox" name="active" value="-1" Checked>Active
			</td>
		</tr>
		<tr>
			<td align="right">Customer Name:</td>
			<td><input type="text" name="custname" size="15" value="Patrick" maxlength="20"></td>
		</tr>
		<tr>
			<td align="right">Password:</td>
			<td><input type="text" name="password" size="15" value="123" maxlength="20"></td>
		</tr><tr>
			<td colspan="2"></td>
		</tr><tr>
			<td align="right">DVR Location:</td>
		</tr>
		<tr>
			<td colspan="2">
				<table width="90%" align="right">

					<tr>
						<td colspan="2" valign="middle">
							<input type="checkbox" name="idLoc_0" value="12" >&nbsp;<b>Test</b>
						</td>
					</tr>
					<tr>
						<td align="right">Channels:</td>
						<td>
							<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr>
							
							<td align="left"><input type="checkbox" name="channelNo_0" value="1" >1</td>
							
							<td align="left"><input type="checkbox" name="channelNo_0" value="2" >2</td>
							
							<td align="left"><input type="checkbox" name="channelNo_0" value="3" >3</td>
							
							<td align="left"><input type="checkbox" name="channelNo_0" value="4" >4</td>
							
							<td align="left"><input type="checkbox" name="channelNo_0" value="5" >5</td>
							
							<td align="left"><input type="checkbox" name="channelNo_0" value="6" >6</td>
							
							<td align="left"><input type="checkbox" name="channelNo_0" value="7" >7</td>
							
							<td align="left"><input type="checkbox" name="channelNo_0" value="8" >8</td>
							
							</tr></table>
						</td>
					</tr>

					<tr>
						<td colspan="2" valign="middle">
							<input type="checkbox" name="idLoc_1" value="13" checked>&nbsp;<b>Test 2</b>
						</td>
					</tr>
					<tr>
						<td align="right">Channels:</td>
						<td>
							<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="1" checked>1</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="2" >2</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="3" checked>3</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="4" >4</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="5" checked>5</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="6" >6</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="7" checked>7</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="8" >8</td>
			</tr><tr>				
							<td align="left"><input type="checkbox" name="channelNo_1" value="9" >9</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="10" >10</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="11" >11</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="12" >12</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="13" >13</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="14" >14</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="15" >15</td>
							
							<td align="left"><input type="checkbox" name="channelNo_1" value="16" >16</td>
							
							</tr></table>
						</td>
					</tr>

				</table>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<HR>
				<input type="hidden" name="lCount" value="1">
				<input type="submit" name="submit" value=" Update ">
				<BR /><BR />
			</td>
		</tr>
		<tr>
			<td colspan="2" bgcolor="#CCCCCC" style="padding:5px">
				<b>Note:</b><BR>
				&bull;&nbsp;Customer's Name and Password will be used to login to Customer DVR site.<BR>
				&bull;&nbsp;DVR system/Location must be selected for its Channels to be accepted.<BR>
				<font color="#FF0000" style="font-size:11px">(DVR and channels must be checked accordingly! Saving data missing one of the two(2) will be skipped.)</font>
			</td>
		</tr>
	</table>
</form>
</p>
	

</div>
</body>
</html>
 
Here is what I hope should be a working example. You can copy/paste it into a new HTML file to test for yourself. See notes below:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"[/URL] />
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>A1 Webcams DVR Admin</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="/includes/admin.css" />
<script src="/includes/x.js" type="text/javascript"></script>
<script type="text/javascript">

 function confirmSubmit() {
     return confirm("This action can not be undone. Are you sure you want to continue?");
 }

 function CheckAll(formObject) {
     var chk = formObject.elements["checkAll"].checked;
     var len = formObject.elements.length;
     for(var i=0; i < len; i++) {
         var elm = formObject.elements[i];
         if (elm.type == "checkbox") {
             elm.checked = chk;
         }
     }
 }

 function confirmFoundSave() {
     return confirm("Match is found. Do you want to continue?");
 }

function validate() {
    if (document.edit.password.value == "") {
        alert("Please enter Password.")
        document.forms["edit"].elements["password"].focus();
        return false;
    }
    if (document.forms["edit"].elements["custname"].value == "") {
        alert("Please enter Customer Name.")
        document.forms["edit"].elements["custname"].focus();
        return false;
    }
    var channelArray, channelChecked, idChecked;
    for (var x = 0; x <= document.forms["edit"].elements["lCount"].value; x++) {
        //get all the channel checkboxes
        channelArray = document.getElementsByName("channelNo_" + x);
        //initially set channelChecked to false before the loop
        channelChecked = false;
        //loop thru the channels to see if one was checked
        for (i = 0; i < channelArray.length && !channelChecked; i++) {
            channelChecked = channelArray[i].checked;
        }
        idChecked = document.forms["edit"].elements["idLoc_" + x].checked
        //if idLoc has been checked, make sure at least one channel has been checked:
        if (idChecked && !channelChecked) {
            alert("Channel has not been selected! Please make your selection and Submit again.")
            return false;
        }
        //if channel was checked, ensure idLoc was checked
        else if (!idChecked && channelChecked) {    
            alert("Location for selected channel(s) has not been checked! Please review your selection and Submit again.")
            return false;
        }
    }
    return true
}

</script>

</head>
<body>
    
    <div id="logo"><img src="/images/adminlogo.jpg" /></div>
    <div id="navigation">
    
            <div class="navlink"><B>&raquo;</B>&nbsp;<a href="default.asp">Home</a></div>
            <div class="navlink"><B>&raquo;</B>&nbsp;<a href="admin.asp">Location(s)</a></div>
            <div class="navlink"><B>&raquo;</B>&nbsp;<a href="admin.asp?view=customers">Customers</a></div>
            <br />
            <div class="navlink"><B>&raquo;</B>&nbsp;<a href="admin_logout.asp">Logout</a></div>
    
    </div>
    <div id="main">
    

<p class="pagetitle">Update Customer</p>
<p class="p12b">

    <a href="admin.asp?view=customers">List ALL Customers</b></a>
    
     | <a href="cust_del.asp?action=del&id=8" onClick="return confirmSubmit()">Remove This Customer</a>

</p>
<p class="p12b">

<BR>
<form name="edit" method="post" action="" onSubmit="return validate();">
    <input type="hidden" name="id" value="8">
    <table border="0" cellpadding="2" cellspacing="0" class="p12b">
        <tr>
            <td align="right" width="20%"><p>&nbsp;</p></td>
            <td               width="80%">
                <input type="checkbox" name="active" value="-1" Checked>Active
            </td>
        </tr>
        <tr>
            <td align="right">Customer Name:</td>
            <td><input type="text" name="custname" size="15" value="Patrick" maxlength="20"></td>
        </tr>
        <tr>
            <td align="right">Password:</td>
            <td><input type="text" name="password" size="15" value="123" maxlength="20"></td>
        </tr><tr>
            <td colspan="2"></td>
        </tr><tr>
            <td align="right">DVR Location:</td>
        </tr>
        <tr>
            <td colspan="2">
                <table width="90%" align="right">

                    <tr>
                        <td colspan="2" valign="middle">
                            <input type="checkbox" name="idLoc_0" value="12" >&nbsp;<b>Test</b>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">Channels:</td>
                        <td>
                            <table width="100%" cellspacing="0" cellpadding="0" border="0"><tr>
                            
                            <td align="left"><input type="checkbox" name="channelNo_0" value="1" >1</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_0" value="2" >2</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_0" value="3" >3</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_0" value="4" >4</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_0" value="5" >5</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_0" value="6" >6</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_0" value="7" >7</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_0" value="8" >8</td>
                            
                            </tr></table>
                        </td>
                    </tr>

                    <tr>
                        <td colspan="2" valign="middle">
                            <input type="checkbox" name="idLoc_1" value="13" checked>&nbsp;<b>Test 2</b>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">Channels:</td>
                        <td>
                            <table width="100%" cellspacing="0" cellpadding="0" border="0"><tr>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="1" checked>1</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="2" >2</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="3" checked>3</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="4" >4</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="5" checked>5</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="6" >6</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="7" checked>7</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="8" >8</td>
            </tr><tr>                
                            <td align="left"><input type="checkbox" name="channelNo_1" value="9" >9</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="10" >10</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="11" >11</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="12" >12</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="13" >13</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="14" >14</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="15" >15</td>
                            
                            <td align="left"><input type="checkbox" name="channelNo_1" value="16" >16</td>
                            
                            </tr></table>
                        </td>
                    </tr>

                </table>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <HR>
                <input type="hidden" name="lCount" value="1">
                <input type="submit" name="submit" value=" Update ">
                <BR /><BR />
            </td>
        </tr>
        <tr>
            <td colspan="2" bgcolor="#CCCCCC" style="padding:5px">
                <b>Note:</b><BR>
                &bull;&nbsp;Customer's Name and Password will be used to login to Customer DVR site.<BR>
                &bull;&nbsp;DVR system/Location must be selected for its Channels to be accepted.<BR>
                <font color="#FF0000" style="font-size:11px">(DVR and channels must be checked accordingly! Saving data missing one of the two(2) will be skipped.)</font>
            </td>
        </tr>
    </table>
</form>
</p>
    

</div>
</body>
</html>
1) you were initially checked the value property of each of the checkboxes, you needed to check the checked property instead
2) you weren't looping thru the list of channels to check if each one was checked, you were only checking the first one. That's why there's the 2nd for loop in the code now.

if you have any questions about what I did feel free to ask.

-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]
 
Again, you saved the day!!
While waiting for your reply, I googled and also realized the checkbox validation is not valid. But until you mentioned, I now know I didn't the second loop to go through all of the Channel checkboxes.

Thank you very much for your help.
Keep up the good work! :)
 
[thumbsup2]

-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