VB Script – How to use VBScript with Forms

VB Script – How to use VBScript with Forms

In this tutorial you will learn about How to use VBScript with Forms. Introduction, How to do validate your Forms and Conclusion

Introduction

Now this is the last topic of the tutorial and you will learn how to validate and submit the data to the web server.

How to do validate your Forms

The process to validate the forms are as under. The two things should be checked.

1. All of the required data is entered.

2. The data entered is valid or not.

We will learn this by an example.

< html >
< head >
< title > Vb Script example< /title >
< script language=”vbscript” >
Option Explicit
Sub cmdSubmit_onClick
‘ Check whether user has entered the required text or not.
If(Len(document.myform.txtage.value)= 0) then
MsgBox “Please enter your age before submitting.”
Exit Sub
End If

‘ Check the data entered is numeric or not.
If ( Not (IsNumeric(document.myform.txtage.value))) then
MsgBox “Please enter numeric data”
Exit Sub
End If

‘Check the range of the age
If (document.myform.txtage.value < 0) or (document.myform.txtage.value > 100) then
Msgbox “Age entered is Invalid.”
Exit Sub
End If

‘If all is fine submit the data
MsgBox “Thanks for providing your age.”
Document.myform.submit
End sub

< /script >
< /head >
< body >
< form name=”myform” >
< table >
< tr >
< td >Please enter your age:< /td >
< td >< Input type=”text” name=”txtage” size=”2” >
< /tr >
< tr >
< td > < /td >
< td >< Input type=”Button” name=”cmdSubmit” value=”Submit” >< /td >
< /tr >
< /table >
< /form >
< /body >
< /html >


In the above example first we are checking whether the user has entered any value or not. If he has not entered any value we will prompt to enter any value. Second validation is that if he has entered any value , just check whether the data entered is numeric or not. If the data is not numeric prompt him to enter the numeric value otherwise move to next step. In the next step we are validating that the age value is entered as per the prescribed range or not.

At last if all the validations are met by the user , he will be shown a message that “Thanks for providing your age” and this data will be submitted to the web server.

Example :


< HTML >
< HEAD >
< TITLE >VBScript example< /TITLE >

< SCRIPT LANGUAGE="VBScript" >
< !– Add this to instruct non-IE browsers to skip over VBScript modules.
Option Explicit

Sub cmdCalculate_OnClick
Dim AmountofDiscount
Dim AmountofTax
Dim DISCOUNT_LIMIT
Dim DISCOUNT_RATE
Dim SubtotalBefore
Dim SubtotalAfter
Dim TAX_RATE
Dim TotalCost

If (Len(Document.myform.txtQuantity.Value) = 0) Then
MsgBox "You must enter a quantity."
Exit Sub
End If

If (Not IsNumeric(Document.myform.txtQuantity.Value)) Then
MsgBox "Quantity must be a numeric value."
Exit Sub
End If

If (Len(Document.myform.cmbProducts.Value) = 0) Then
MsgBox "You must select a product."
Exit Sub
End If

‘ Define our constant values.
DISCOUNT_LIMIT = 1000
DISCOUNT_RATE = .10
TAX_RATE = 0.06

‘ Calculate the subtotal for the order.
SubtotalBefore = Document.myform.txtQuantity.Value * Document.myform.lblUnitCost.Caption

‘ Check to see if the order is large enough to offer discounts.
If (SubtotalBefore > DISCOUNT_LIMIT) Then
AmountofDiscount = SubtotalBefore * DISCOUNT_RATE
Else
AmountofDiscount = 0
End If
SubtotalAfter = SubtotalBefore – AmountofDiscount

‘ Calculate taxes and total cost.
AmountofTax = SubtotalAfter * TAX_RATE
TotalCost = SubtotalAfter + AmountofTax

‘ Display the results.
Document.myform.lblSubtotalBefore.Caption = SubtotalBefore
Document.myform.lblDiscount.Caption = AmountofDiscount
Document.myform.lblSubtotalAfter.Caption = SubtotalAfter
Document.myform.lblTaxes.Caption = AmountofTax
Document.myform.lblTotalCost.Caption = TotalCost

End Sub

Sub cmdSubmit_onClick
‘ Submit this order for processing.
MsgBox "Your order has been submitted."
Document.myform.Submit
End Sub

Sub cmbProducts_Change()
Select Case Document.myform.cmbProducts.Value
Case "NEC MultiSync E1100"
Document.myform.lblUnitCost.Caption = 1590
Case "NEC MultiSync P1150"
Document.myform.lblUnitCost.Caption = 880
Case "NEC MultiSync E750"
Document.myform.lblUnitCost.Caption = 1940
Case Else
Document.myform.lblUnitCost.Caption = 0
End Select
End Sub

— >
< /SCRIPT >

< /HEAD >

< BODY >
< FORM NAME="myform" >
< TABLE >
< TR >
< TD >< B >Monitor:< /B >< /TD >
< TD >
< OBJECT ID="cmbProducts" WIDTH=159 HEIGHT=24
CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002F3" >
< PARAM NAME="DisplayStyle" VALUE="7" >
< PARAM NAME="Size" VALUE="4202;635" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="MatchEntry" VALUE="1" >
< PARAM NAME="ShowDropButtonWhen" VALUE="2" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Quantity:< /B >< /TD >
< TD >
< OBJECT ID="txtQuantity" WIDTH=100 HEIGHT=24
CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002F3" >
< param name="VariousPropertyBits" value="746604571" >
< param name="BackColor" value="2147483653" >
< param name="ForeColor" value="2147483656" >
< param name="MaxLength" value="0" >
< param name="BorderStyle" value="0" >
< param name="ScrollBars" value="0" >
< param name="DisplayStyle" value="1" >
< param name="MousePointer" value="0" >
< param name="Size" value="2646;635" >
< param name="PasswordChar" value="0" >
< param name="ListWidth" value="0" >
< param name="BoundColumn" value="1" >
< param name="TextColumn" value="65535" >
< param name="ColumnCount" value="1" >
< param name="ListRows" value="8" >
< param name="cColumnInfo" value="0" >
< param name="MatchEntry" value="2" >
< param name="ListStyle" value="0" >
< param name="ShowDropButtonWhen" value="0" >
< param name="ShowListWhen" value="1" >
< param name="DropButtonStyle" value="1" >
< param name="MultiSelect" value="0" >
< param name="Value" value >
< param name="Caption" value >
< param name="PicturePosition" value="458753" >
< param name="BorderColor" value="2147483654" >
< param name="SpecialEffect" value="2" >
< param name="Accelerator" value="0" >
< param name="GroupName" value >
< param name="FontName" value="MS Sans Serif" >
< param name="FontEffects" value="1073741824" >
< param name="FontHeight" value="200" >
< param name="FontOffset" value="0" >
< param name="FontCharSet" value="0" >
< param name="FontPitchAndFamily" value="2" >
< param name="ParagraphAlign" value="2" >
< param name="FontWeight" value="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< INPUT TYPE="Button" NAME="cmdCalculate" VALUE="Calculate Cost" >< /TD >
< TD >< /TD >
< /TR >
< TR >
< TD >< B >Unit Cost:< /B >< /TD >
< TD >
< OBJECT ID="lblUnitCost" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< param name="ForeColor" value="0" >
< param name="BackColor" value="16777215" >
< param name="VariousPropertyBits" value="8388635" >
< param name="Caption" value >
< param name="PicturePosition" value="458753" >
< param name="Size" value="2646;635" >
< param name="MousePointer" value="0" >
< param name="BorderColor" value="2147483654" >
< param name="BorderStyle" value="0" >
< param name="SpecialEffect" value="2" >
< param name="Accelerator" value="0" >
< param name="FontName" value="MS Sans Serif" >
< param name="FontEffects" value="1073741824" >
< param name="FontHeight" value="200" >
< param name="FontOffset" value="0" >
< param name="FontCharSet" value="0" >
< param name="FontPitchAndFamily" value="2" >
< param name="ParagraphAlign" value="2" >
< param name="FontWeight" value="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Subtotal before discount:< /B >< /TD >
< TD >
< OBJECT ID="lblSubtotalBefore" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< param name="ForeColor" value="0" >
< param name="BackColor" value="16777215" >
< param name="VariousPropertyBits" value="8388635" >
< param name="Caption" value >
< param name="PicturePosition" value="458753" >
< param name="Size" value="2646;635" >
< param name="MousePointer" value="0" >
< param name="BorderColor" value="2147483654" >
< param name="BorderStyle" value="0" >
< param name="SpecialEffect" value="2" >
< param name="Accelerator" value="0" >
< param name="FontName" value="MS Sans Serif" >
< param name="FontEffects" value="1073741824" >
< param name="FontHeight" value="200" >
< param name="FontOffset" value="0" >
< param name="FontCharSet" value="0" >
< param name="FontPitchAndFamily" value="2" >
< param name="ParagraphAlign" value="2" >
< param name="FontWeight" value="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Discount:< /B >< /TD >
< TD >
< OBJECT ID="lblDiscount" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< param name="ForeColor" value="0" >
< param name="BackColor" value="16777215" >
< param name="VariousPropertyBits" value="8388635" >
< param name="Caption" value >
< param name="PicturePosition" value="458753" >
< param name="Size" value="2646;635" >
< param name="MousePointer" value="0" >
< param name="BorderColor" value="2147483654" >
< param name="BorderStyle" value="0" >
< param name="SpecialEffect" value="2" >
< param name="Accelerator" value="0" >
< param name="FontName" value="MS Sans Serif" >
< param name="FontEffects" value="1073741824" >
< param name="FontHeight" value="200" >
< param name="FontOffset" value="0" >
< param name="FontCharSet" value="0" >
< param name="FontPitchAndFamily" value="2" >
< param name="ParagraphAlign" value="2" >
< param name="FontWeight" value="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Subtotal after discount:< /B >< /TD >
< TD >
< OBJECT ID="lblSubtotalAfter" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< param name="ForeColor" value="0" >
< param name="BackColor" value="16777215" >
< param name="VariousPropertyBits" value="8388635" >
< param name="Caption" value >
< param name="PicturePosition" value="458753" >
< param name="Size" value="2646;635" >
< param name="MousePointer" value="0" >
< param name="BorderColor" value="2147483654" >
< param name="BorderStyle" value="0" >
< param name="SpecialEffect" value="2" >
< param name="Accelerator" value="0" >
< param name="FontName" value="MS Sans Serif" >
< param name="FontEffects" value="1073741824" >
< param name="FontHeight" value="200" >
< param name="FontOffset" value="0" >
< param name="FontCharSet" value="0" >
< param name="FontPitchAndFamily" value="2" >
< param name="ParagraphAlign" value="2" >
< param name="FontWeight" value="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Taxes:< /B >< /TD >
< TD >
< OBJECT ID="lblTaxes" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< param name="ForeColor" value="0" >
< param name="BackColor" value="16777215" >
< param name="VariousPropertyBits" value="8388635" >
< param name="Caption" value >
< param name="PicturePosition" value="458753" >
< param name="Size" value="2646;635" >
< param name="MousePointer" value="0" >
< param name="BorderColor" value="2147483654" >
< param name="BorderStyle" value="0" >
< param name="SpecialEffect" value="2" >
< param name="Accelerator" value="0" >
< param name="FontName" value="MS Sans Serif" >
< param name="FontEffects" value="1073741824" >
< param name="FontHeight" value="200" >
< param name="FontOffset" value="0" >
< param name="FontCharSet" value="0" >
< param name="FontPitchAndFamily" value="2" >
< param name="ParagraphAlign" value="2" >
< param name="FontWeight" value="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Total Cost:< /B >< /TD >
< TD >
< OBJECT ID="lblTotalCost" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< param name="ForeColor" value="0" >
< param name="BackColor" value="16777215" >
< param name="VariousPropertyBits" value="8388635" >
< param name="Caption" value >
< param name="PicturePosition" value="458753" >
< param name="Size" value="2646;635" >
< param name="MousePointer" value="0" >
< param name="BorderColor" value="2147483654" >
< param name="BorderStyle" value="0" >
< param name="SpecialEffect" value="2" >
< param name="Accelerator" value="0" >
< param name="FontName" value="MS Sans Serif" >
< param name="FontEffects" value="1073741824" >
< param name="FontHeight" value="200" >
< param name="FontOffset" value="0" >
< param name="FontCharSet" value="0" >
< param name="FontPitchAndFamily" value="2" >
< param name="ParagraphAlign" value="2" >
< param name="FontWeight" value="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< INPUT TYPE="Button" NAME="cmdSubmit" VALUE="Submit Order" >< /TD >
< TD >< /TD >
< /TR >
< /TABLE >
< /FORM >

< SCRIPT LANGUAGE="VBScript" >
< !–
Document.myform.cmbProducts.Additem "NEC MultiSync E1100"
Document.myform.cmbProducts.Additem "NEC MultiSync P1150"
Document.myform.cmbProducts.Additem "NEC MultiSync E750"
— >
< /SCRIPT >
< /BODY >

< /HTML >


The output on the browser would be as follows.



In the scripting part we have validated the Quantity field and determined that whether the user has entered some value or not. We have used the inbuilt vbscript function called Len which checks the length of the user input and compares.If the length comes to zero the user is prompted with the message. Second part we have checked for the numeric value whether the user has entered a numeric value or not. If we have passed all those values than the cost of the order is calculated and displayed.

Conclusion

This all wraps up our vbscript tutorials . Till now you must be aware of the basics and how to use vbscript with your web pages.

[catlist id=168].

Related posts