VBS TUTORIAL: VBScript and Form _vbs

Source: Internet
Author: User
Tags object model

VBScript and Forms

Simple validation

With Visual Basic scripting Edition, you can accomplish a large amount of form processing that you typically do on a server, or you can do work that you cannot do on the server.

This is a simple example of client-side validation. The result of the HTML code is a text box and a button. If you use Microsoft Internet Explorer to view a page made with the following code, you will see a small text box with a button next to it.

<HTML><HEAD><TITLE>Simple validation</TITLE><SCRIPT LANGUAGE="VBScript"> <!--Sub Validate Dim TheForm Set TheForm = Document.forms("ValidForm") If IsNumeric(TheForm.Text1.Value) Then If TheForm.Text1.Value < 1 Or TheForm.Text1.Value > 10 Then MsgBox "Please enter a 1 To 10 Between the numbers." Else MsgBox "Thank you." End If Else MsgBox "Please enter a number." End IfEnd Sub--></SCRIPT></HEAD><BODY><H3>Simple validation</H3><HR><form id="ValidForm" action="nothing.asp" onsubmit="Validate(); return false;" language="Visual Basic Scripting Edition">Please enter a 1 To 10 The number between:<input name="Text1" TYPE="TEXT" class='9v'><input name="Submit" TYPE="Submit" VALUE="Submit"></form></BODY></HTML>

The difference between this text box and the sample in the simple example of a VBScript page is that the Value property of the text box is used to check the input value. To use the Value property of a text box, the code must refer to the name of the text box.

Each time you reference a text box, you should write the full name, that is, Document.ValidForm.Text1. However, when you refer to a form control multiple times, you can follow these steps: First declare a variable, and then assign the form Document.validform to the variable theform using the Set statement, so that you can use THEFORM.TEXT1 to reference the text box. A regular assignment statement, such as Dim, is not valid here, and you must use Set to keep a reference to an object.

Working with numbers

Note that the above example directly detects 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 practice to detect data subtypes of user input values and, if necessary, to use conversion functions. When adding with the Value property of a text box, you should explicitly convert it to a number because the plus (+) operator is not only additive-able, but also string-connected. For example, if the Text1 contains "1" and the Text2 contains "2", you will see the following results:

A = Text1.Value + Text2.Valueto " 12 " A = CDbl(Text1.Value) + Text2.Value  as 3

Pass data back to the server after validation

A simple validation sample uses a normal button control. If you use the Submit control, all data is immediately routed to the server, and the sample will not see the data for checking. Avoid using the submit control so that you can examine the data, but you cannot submit the data to the server. If you want to submit the data, you need to add another line of code, as follows:

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

When the data is entered correctly, the code calls the Form object's Submit method to pass the data to the server. The server processes data, whether correct or not, unless it is judged to be right or wrong before the data is passed to the server. You can find all the information about the Submit method and other methods on the Internet Explorer Scripting Object Model page, which is on the Microsoft (R) Web site (http:// www.microsoft.com) can be found on.

So far, you've only seen standard HTML <FORM> objects. Internet Explorer also enables you to create pages using the full functionality of the ActiveX (R) control (formerly called an OLE control) and the Java (TM) 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.