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!

For each control in Controls 3

Status
Not open for further replies.

rtshort

IS-IT--Management
Feb 28, 2001
878
US
Will the "for each control in controls" work in VBScript.

I have a lot of text boxes that I want to make sure certain characters are not in before submitting a form.

Rob
Just my $.02.
 

try this in vbscript
Dim ctl As Control

For Each ctl In Me.Controls

MsgBox ctl.Name
Next
 
Yes, VBS supports the For Each statement.

However, it does NOT support strong typing as chmohan's post illustrates.

Jon Hawkins
 
VBSCript + form... I smell Internet Explorer.

Am I right? This is client-side script hosted in IE?

If so, there are a number of things you can do. Easiest is to simply give all of the &quot;text boxes&quot; (I assume you mean type=text <input> form elements and/or <textarea> elements) the same id attribute value. This makes a &quot;collection&quot; out of them, allowing the kind of thing you want. Almost like a VB control array. You can even use multiple different id values to indicate different types of validation to perform.

Remember, the name attribute is used for form get/post actions, the id is what you use to identify an element within the browser. Yes, sometimes you can set name and treat it as id - avoid this.

Crude example:
Code:
<html>
  <head>
    <title>For Each Text</title>
    <script language=&quot;vbscript&quot;>
      Function EditNonEmpty(ByVal strVal)
        EditNonEmpty = Trim(strVal) = &quot;&quot;
      End Function

      Function EditNumeric(ByVal strVal)
        EditNumeric = Trim(strVal) = &quot;&quot; Or Not IsNumeric(Trim(strVal))
      End Function

      Sub ResetForm(blnErase)
        Dim txtElem

        ' Clear all <input type=&quot;text&quot;> form elements.
        For Each txtElem In frmDemo.all.tags(&quot;input&quot;)
          If txtElem.type = &quot;text&quot; Then
            If blnErase Then
              txtElem.value = &quot;&quot;
            End If
            txtElem.style.backgroundColor = &quot;&quot;
          End If
        Next
      End Sub

      Sub btnSubmit_onclick()
        Dim txtElem, obj1stErr

        ResetForm False

        ' Validate multiple id=&quot;txtNonEmpty&quot; form elements.
        For Each txtElem In frmDemo.txtNonEmpty
          If EditNonEmpty(txtElem.value) Then
            txtElem.style.backgroundColor = &quot;#ffcccc&quot;
            If Not IsObject(obj1stErr) Then Set obj1stErr = txtElem
          End If
        Next

        ' Validate single id=&quot;txtNumeric&quot; form element.
        If EditNumeric(frmDemo.txtNumeric.value) Then
          frmDemo.txtNumeric.style.backgroundColor = &quot;#ffcccc&quot;
          If Not IsObject(obj1stErr) Then Set obj1stErr = frmDemo.txtNumeric
        End If

        If IsObject(obj1stErr) Then
          divMessage.innerText = &quot;Oops, fill things in correctly please!&quot;
          obj1stErr.focus
          Set obj1stErr = Nothing
        Else
          divMessage.innerText = &quot;&quot;
          ResetForm True
          frmDemo.txtNonEmpty(0).focus
        End If
      End Sub

      Sub window_onload()
        ' Set focus to 1st field.  Since there are multiple elements with
        ' the same id value, we have a collection and must index the
        ' one we want.
        frmDemo.txtNonEmpty(0).focus
      End Sub
    </script>
  </head>
  <body>
    <form name=&quot;frmDemo&quot; id=&quot;frmDemo&quot;>
      <table border=&quot;0&quot; width=&quot;400&quot;>
        <tr>
          <td align=&quot;right&quot;>First Name</td>
          <td><input type=&quot;text&quot; name=&quot;FirstName&quot; id=&quot;txtNonEmpty&quot;
               tabindex=&quot;1&quot; size=&quot;15&quot; maxlength=&quot;15&quot;></td>
        </tr>
        <tr>
          <td align=&quot;right&quot;>Last Name</td>
          <td><input type=&quot;text&quot; name=&quot;LastName&quot; id=&quot;txtNonEmpty&quot;
               tabindex=&quot;2&quot; size=&quot;15&quot; maxlength=&quot;15&quot;></td>
        </tr>
        <tr>
          <td align=&quot;right&quot;>Address 1</td>
          <td><input type=&quot;text&quot; name=&quot;Address1&quot; id=&quot;txtNonEmpty&quot;
               tabindex=&quot;3&quot; size=&quot;30&quot; maxlength=&quot;30&quot;></td>
        </tr>
        <tr>
          <td align=&quot;right&quot;>Address 2</td>
          <td><input type=&quot;text&quot; name=&quot;Address2&quot;
               tabindex=&quot;4&quot; size=&quot;30&quot; maxlength=&quot;30&quot;></td>
        </tr>
        <tr>
          <td align=&quot;right&quot;>City</td>
          <td>
            <input type=&quot;text&quot; name=&quot;City&quot; id=&quot;txtNonEmpty&quot;
             tabindex=&quot;5&quot; size=&quot;15&quot; maxlength=&quot;15&quot;>
            State <input type=&quot;text&quot; name=&quot;State&quot; id=&quot;txtNonEmpty&quot;
                   tabindex=&quot;6&quot; size=&quot;2&quot; maxlength=&quot;2&quot;>
            Zip <input type=&quot;text&quot; name=&quot;Zip&quot; id=&quot;txtNumeric&quot;
                 tabindex=&quot;7&quot; size=&quot;5&quot; maxlength=&quot;5&quot;>
          </td>
        </tr>
        <tr>
          <td align=&quot;right&quot;><input type=&quot;button&quot; id=&quot;btnSubmit&quot; value=&quot;Submit&quot;
                             tabindex=&quot;8&quot;></td>
          <td><div id=&quot;divMessage&quot;
               style=&quot;color:red; font: bold 12pt Arial&quot;></div></td>
        </tr>
      </table>
    </form>
  </body>
</html>
Was this sort of what you were after? Remember, it isn't portable to other browsers. Probably only IE versions > 3.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top