VBScript Basic teaching 11 VBScript with form

Source: Internet
Author: User
Tags contains object model reference variable
Vbscript| Basic Tutorial 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 are using Microsoft? Internet Explorer looks at a page made with the following code, and you see a small text box with a button next to it.

<HTML>
<HEAD><TITLE> Simple Validation </TITLE>
<script language= "VBScript" >
<!--
Sub Submit_onclick
Dim theform
Set theform = Document.validform
If IsNumeric (TheForm.Text1.Value) Then
If TheForm.Text1.Value < 1 Or TheForm.Text1.Value > Then
MsgBox "Please enter a number from 1 to 10." "
Else
MsgBox "Thank you." "
End If
Else
MsgBox "Please enter a number. "
End If
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H3> Simple Validation </H3><HR>
<form name= "ValidForm" >
Please enter a number from 1 to 10:
<input name= "Text1" type= "TEXT" size= "2" >
<input name= "Submit" type= "button" value= "submitted" >
</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.value ' A is "12"
A = CDBL (text1.value) + Text2.value ' A is 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 Submit_onclick
Dim theform
Set theform = Document.validform
If IsNumeric (TheForm.Text1.Value) Then
If TheForm.Text1.Value < 1 Or TheForm.Text1.Value > Then
MsgBox "Please enter a number from 1 to 10." "
Else
MsgBox "Thank you." "
Theform.submit ' data is entered correctly and passed to the server.
End If
Else
MsgBox "Please enter a number. "
End If
End 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 Script Object Model page.

So far, you've only seen standard HTML <FORM> objects. Internet Explorer can also enable you to take advantage of ActiveX? Control (formerly known as an OLE control) and Java? Creates a page with all of the functionality of an 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.