VBScript tutorial Lesson 13th VBScript and forms

Source: Internet
Author: User

Simple Verification

With Visual Basic Scripting Edition, you can perform a lot of form processing work on the server, or do not work on the server.

This is a simple example of client verification. The result of the HTML code is a text box and a button. If Microsoft? Internet Explorer displays the page created with the following code. You will see a text box with a button next to it.

<HTML>
<HEAD> <TITLE> simple verification </TITLE>
<Script language = "VBScript">
<! --
Sub Submit_nClick
Dim TheForm
Set TheForm = Document. ValidForm
If IsNumeric (TheForm. Text1.Value) Then
If TheForm. Text1.Value <1 Or TheForm. Text1.Value> 10 Then
MsgBox "enter a number between 1 and 10. "
Else
MsgBox "Thank you. "
End If
Else
MsgBox "enter a number. "
End If
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H3> simple verification </H3> <HR>
<Form name = "ValidForm">
Enter a number between 1 and 10:
<Input name = "Text1" TYPE = "TEXT" SIZE = "2">
<Input name = "Submit" TYPE = "BUTTON" VALUE = "Submit">
</FORM>
</BODY>
</HTML>

The difference between this text box and the simple example on the VBScript page is that the Value attribute of the text box is used to check the input Value. To use the Value attribute of a text box, the Code must reference the name of the text box.

The full name should be written every time the text box is referenced, that is, Document. ValidForm. Text1. However, when you reference the form control multiple times, follow these steps: declare a variable first, and then use the Set statement to import the form Document. validForm is assigned to the variable TheForm so that TheForm can be used. text1 references the text box. The general value assignment statement (such as Dim) is invalid here. You must use Set to maintain reference to the object.

Use numbers

Note that the preceding example directly checks whether the input value is a number: Use the IsNumeric function to determine whether the string in the text box is a number. Although VBScript can automatically convert strings and numbers, it is always a good habit to detect sub-types of user input values and use conversion functions when necessary. When using the Value Attribute of the text box for addition operations, it should be explicitly converted to a number, because the plus sign (+) operator can not only perform addition operations, and can be connected to strings. For example, if Text1 contains "1" and Text2 contains "2", you will see the following results:

A = Text1.Value + Text2.Value 'a is "12"
A = CDbl (Text1.Value) + Text2.Value 'a is 3

Pass data back to the server after verification

The simple verification example uses a common button control. If you use the Submit control, all data will be immediately transmitted to the server, and the sample will not see the data for inspection. Avoid using the Submit control so that you can check data, but cannot Submit data to the server. If you want to submit data, you need to add another line of code, as shown below:

<Script language = "VBScript">
<! --
Sub Submit_nClick
Dim TheForm
Set TheForm = Document. ValidForm
If IsNumeric (TheForm. Text1.Value) Then
If TheForm. Text1.Value <1 Or TheForm. Text1.Value> 10 Then
MsgBox "enter a number between 1 and 10. "
Else
MsgBox "Thank you. "
TheForm. Submit: the data is entered correctly and passed to the server.
End If
Else
MsgBox "enter a number. "
End If
End Sub
-->
</SCRIPT>

When the data is entered correctly, the Code calls the Submit method of the form object to pass the data to the server. The server processes the data, regardless of whether the data is correct or not, unless it is determined that the data is correct before it is passed to the server. You can find all information about the Submit method and other methods on the Internet Explorer Script Object Model page.

So far, you have only seen the standard HTML <FORM> object. Internet Explorer also allows you to use ActiveX? Controls (formerly known as OLE controls) and Java? Page for creating all functions of the object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.