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

Loops and Arrays in Validation

Status
Not open for further replies.

TheForge

Programmer
Nov 20, 2002
4
CA
First off, Im new to programming so go easy on me :).

I have created an array of strings with all of my html input box names in order to ensure no empty fields. I want to check these via a while loop (any loop wll do). But when I try to input my array name and index number it tells me that "Object does not support this method or property".

Here is a piece of the code.
veriForm = form name
allFields = array name

Code:
counter = 0
While counter < allFields.Ubound()
      if (veriForm.allFields(counter).value =&quot;&quot;)Then
        MsgBox &quot;All fields are required!&quot;
        flag = false
        counter = allFields.Ubound()
      else
        counter = counter + 1
      end if
Wend
I would be grateful for any amount of help....Thanks.
 
Try it this way:
alertStr = &quot;&quot;

FOR i = 0 to document.myForm.elements.length - 1
select case uCase(document.myForm.elements(i).type)
case &quot;TEXT&quot;, &quot;SELECT&quot;,
IF document.myForm.elements(i).value = &quot;&quot; THEN
alertStr = alertStr & vbcrlf & &quot;- &quot; & document.myForm.elements(i).name
end if
end select
NEXT

if alertStr <> &quot;&quot; then
alert &quot;You need to fill the following:&quot; & alertStr
exit sub
end if


Here's another method which highlights required fields....

function checkForm()
checkForm = true
for i = 0 to document.CBR.elements.length -1
select case i
case 0,1
if document.cbr.elements(0).checked = false and document.cbr.elements(1).checked = false then
document.all.cbr.elements(0).style.background = &quot;yellow&quot;
document.all.cbr.elements(1).style.background = &quot;yellow&quot;
checkForm = false
else
document.all.cbr.elements(0).style.background = vbdefault
document.all.cbr.elements(1).style.background = vbdefault
end if
case 2,3,8,9,10,13
if document.cbr.elements(i).value = &quot;&quot; then
checkForm = false
document.all.cbr.elements(i).style.background = &quot;yellow&quot;
end if
case 6,7
if document.cbr.elements(6).checked = false and document.cbr.elements(7).checked = false then
document.all.cbr.elements(6).style.background = &quot;yellow&quot;
document.all.cbr.elements(7).style.background = &quot;yellow&quot;
checkForm = false
else
document.all.cbr.elements(6).style.background = vbdefault
document.all.cbr.elements(7).style.background = vbdefault
end if
case 4,5,12 'one of these must be filled out'
if document.cbr.elements(4).value = &quot;&quot; and document.cbr.elements(5).value = &quot;&quot; and document.cbr.elements(12).value = &quot;&quot; then
checkForm = false
if document.cbr.elements(i).disabled = false then
document.all.cbr.elements(i).style.background = &quot;yellow&quot;
end if
end if
end select
next
if checkForm = false then
alert &quot;Please fill in these required spaces!&quot;
end if
end function



NEXT -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
FOR i = 0 to document.myForm.elements.length - 1
select case uCase(document.myForm.elements(i).type)
case &quot;TEXT&quot;, &quot;SELECT&quot;, <-- oops!, extra comma - also note that this works in IE only.....
IF document.myForm.elements(i).value = &quot;&quot; THEN
alertStr = alertStr & vbcrlf & &quot;- &quot; & document.myForm.elements(i).name
end if
end select
NEXT
-- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
Thanks alot, couple of areas I'm a bit unsure about.

document.myform.element

document- Is this a variable? Or is it a function of VBS?

element- this is where I would input my array name right?


Sorry if these questions seem overly simple....but I am completly new to this.

Thanks again for your help.
 
document is object for the entire page. I didn't work w/ your code at all - I tried to give you a completely new solution.

You don't need to create any array - it already exists:

document.myForm.elements = array of elements on the form &quot;myForm&quot;
-- remember that this includes buttons and hidden elements
WITH document.myForm.elements(i)
.name = the element's name
.value = the element's value
.type = the element's input type
END WITH

Thie first code I wrote looks at form elements based on their input type. It then compiles a list of all blank text boxes or selects. If any are found, it displays a list of incomplete elements (by their name).

The second code looks at form elements based on their position in the form. Any required fields that are incomplete are highlighted. -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
Cool, no need to make an array.
That is a neat little tool.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top