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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to validate elements without knowing Item names

Status
Not open for further replies.

mts176

MIS
Apr 17, 2001
109
US
I need to make sure there is information contained within various flavors of inputs. I already have the Dropdown and textboxes working, but I can't get the radio buttons and check boxes.

I want to make sure they are checked. How do I go about doing that. Below is my validation routine and my actual input boxes. Thanks.

Code:
	Sub ChkSubmit(nquestions)
	dim results, i,DEname,DEvalue,DEtype,iLoop
	i = 0
		'results = msgbox("Hello   "& nquestions)

		Do while i <= nquestions
			
			DEtype = document.frmDemogFormsAction.elements(i).type
			DEname = document.frmDemogFormsAction.elements(i).name
			DEvalue = document.frmDemogFormsAction.elements(i).value
			results = msgbox("Please enter information for "& DEvalue & " : " &DEname & " : " & DEtype)
			
			
			Select case DEtype
				Case "select-one"
					If cdbl(DEvalue) = -1 then
						results = msgbox("Please enter information for "& DEname)
						'document.frmDemogFormsAction.elements(i).focus
						'exit sub
					End if
				case "radio"
				'	For iLoop = 0 to 1
				'		If document.frmDemogFormsAction.radio(iLoop).checked = True Then
				'			results = msgbox("Please enter information for "& DEvalue & "  " &DEname)
				'			Exit For
				'		End	If
				'	Next
				case "text"
					If DEvalue = "" then
						results = msgbox("Please enter information for "& DEname)
						'document.frmDemogFormsAction.elements(i).focus
						'exit sub
					End if
				case "checkbox"
					If DEvalue = off then
						results = msgbox("Please enter information for "& DEname)
						'document.frmDemogFormsAction.elements(i).focus
						'exit sub
					End if
			end select
			
			'If cdbl(DEvalue) = -1 or DEvalue = "" then				
				
				'document.frmDemogFormsAction.elements(i).focus
				'exit sub
			'End if
			
			
		i = i + 1
		loop
	End Sub


Case "Checkbox"
					'//Inserts a Checkbox if the Response type is "Checkbox"
					%><input type=checkbox name=<%=rsGQ.Fields("VC_FORM_FIELD_NAME")%>>&nbsp;&nbsp;<%=rsGA.Fields("VC_ANSWER_TEXT")%><%
				Case "radio"
					'//Inserts a set of Radio Buttons if the Response type is "radio"
					%><input type=radio name=<%=rsGQ.Fields("VC_FORM_FIELD_NAME")%> value=1><%=rsGA.Fields("VC_ANSWER_TEXT")%>
					<%rsGA.MoveNext%>
					  <input type=radio name=<%=rsGQ.Fields("VC_FORM_FIELD_NAME")%> value=2><%=rsGA.Fields("VC_ANSWER_TEXT")%><%
 
There is reason that the script won't work fundamentally. The radio button set which you want to loop 0,1... cannot be referred to like this. Each an every radio button occupied one of the places in elements(i). Similarly for checkbox. To illustrate this simple fact, here is a demo.
Code:
<html>
<head>
<script language="vbscript">
function checkit()
    set celem=document.frmtest.elements
    msgbox celem.length
    for i=0 to celem.length-1
        'for text value is empty, for radio and checkbox the value is on
        with celem.item(i)
            msgbox i & vbcrlf & .type & vbcrlf & .name & vbcrlf & .value
        end with
    next
end function
</script>
</head>
<body onload="checkit()">
<form name="frmtest">
<input type="text" name="a" id="a" /><br />
<input type="radio" name="b" id="b1" /><br />
<input type="radio" name="b" id="b2" /><br />
<input type="checkbox" name="c" id="c1" /><br />
<input type="checkbox" name="c" id="c2" /><br />
</form>
</body>
</html>
To get hold of the radio input(s), either use getElementsByTagName() for radio button set's name, or can try to use getElementsByTagName("RADIO"), but then it contains buttons outside of certain mutually excluding choices. (Similar reasoning for checkboxes.)

- tsuji
 
I am trying to use "getElementsByTagName("RADIO")" but my object is still empty.

This is the start of my code but it never gets into the For statement.

Code:
Set oElements = frmDemogFormsAction.getElementsByTagName("radio")
		' Iterate collection looking for selected radio button		
		For Each oElement in oElements
			Select case (oElement.type)
				case "radio"

I am getting the radio button names out of a database so I wont know what they are called, is there any way to just get all of the radio buttons on the page.

Second question:

Is there any way to go forward or backward with the for next statement.

With recordsets they have movenext and moveprevious, I want to move to the next element for a comparison but I dont want to get out of sync for the rest of my code. I just want to compare not actually move ahead.
 
mts176,

Sorry! That bit of my posting getElementsByTagName("radio") is wrong. I take it back.

I should had put in that context ("input") instead to be correct. But then, the return collection needs further filtering down by .type="radio".

- tsuji

 
Just as an FYI, this is how I solved my problem
Code:
	Sub ChkSubmit()
		dim results, iCount, bFlag
		DIM cName, pName
		Dim oElement, oElements
		
		cName = ""  '//current input element name
		pName = ""  '//previous input element name

		bFlag = False '//determines whether or not there was an error in processing the inputs
		iCount = 0
		' Get reference to Elements collection
		Set oElements = Document.frmDemogFormsAction.Elements
		
		' Iterate collection looking for selected radio button		
		For Each oElement in oElements
			iCount =  iCount + 1
			'//set a variable to the first element name, this will be used to compare to other elements
			'//on the page.  If previous element name <> current element name without any
			'//element being selected, then diplay a message box and exit sub routine.
			if pName="" then pName=oElement.name
			cName=oElement.name
			
			'//removes numbers after the name of the input device, this is mostly for checkbox
			'//they are the only ones that can have multiple selections
			'//This has to be done so the checkboxes can still act as one solid group instead of individuals
			if IsNumeric(right(pName,3)) then pName=left(pName,len(pName)-3)
			if IsNumeric(right(pName,2)) then pName=left(pName,len(pName)-2)
			if IsNumeric(right(pName,1)) then pName=left(pName,len(pName)-1)
		
			if IsNumeric(right(cName,3)) then cName=left(cName,len(cName)-3)
			if IsNumeric(right(cName,2)) then cName=left(cName,len(cName)-2)
			if IsNumeric(right(cName,1)) then cName=left(cName,len(cName)-1)
			
			
			'//compare the current radio button name with the previous radio button name if they are not equal
			'//and the flag = false display a message and exit sub routine. If Previous and Current are not equal
			'//and the flag = true then continue on with the rest of the validation.  Set Previous = Current to
			'//start the process over again.
			if cName<>pName then
				if bFlag = false then
					results = msgbox("Please enter information for " & pName)
					exit sub
				end if 
				pName=cName
				bFlag = False
			end if
			
			Select case (oElement.type)
				case "radio"
					'//set a flag if flag = true then a radio button has been selected
					'//if flag = false then diplay a message box and exit sub routine.
					if oElement.Checked = True then
						bFlag = True
					else
						'//do not allow the flag to be set to false if already set to true for this group of radio buttons
						if bFlag = False then bFlag = False
					end if
				Case "select-one"
					'//check to make sure the user selected a valid answer from the dropdown selection
					If cdbl(oElement.value) = -1 then
						bFlag = false
					else
						bFlag = True
					End if
				case "text"
					'//if the text box is empty set the flag to false
					If oElement.value = "" then
						bFlag = false
					else
						'//if the textbox is not empty check for valid input
						'//If function returns true then user is entering special characters
						'//display a message, select and set focus on the textbox in question
						'//do not finish rest of validation until the problem is fixed
						if EditText(iCount-1) then
							msgbox "You can only use alphabetic characters, dashes, spaces, numbers, slashes, and single quotes in textboxes"
							document.frmDemogFormsAction.Elements(iCount-1).select
							document.frmDemogFormsAction.Elements(iCount-1).focus
							exit sub
						End if
						bFlag = True
					End if
				case "checkbox"
						'//set a flag, if flag = true then a checkbox has been selected
						'//if flag = false then diplay a message box and exit sub routine.
						if oElement.Checked = True then
							bFlag = True
						else
							'//do not allow the flag to be set to false if already set to true for this group of checkboxes
							if bFlag = False then bFlag = False
						end if					
				case else '//the case else is for buttons and hidden text, no need to prompt user on what to do with these
					bFlag = true
			end select
		Next		
		'//acts as the submit button
		document.frmDemogFormsAction.submit		
	End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top