Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
VBS Basics
Introduction
Variables & Arrays
Sub & Function Procedure
If Statements
Case Statements
Loop Statements
Operators
Forms
MsgBox Function
Books @
CA Amazon
UK Amazon
US Amazon
 

VB Script Form Validation

Form validation involves checking if the required information is provided by the user. We can make sure to see if a field is empty, figure out type of value provided, count number of characters or value, can check if special character(s) is present and more.
Here is a syntax for checking a field value.
if form.name.value="" then msgbox"Please enter name". This checks if the name field is empty and informs the user.
if len(form.name.value) < 2 or len(form.txtname.value)>50 then msgbox "Too short or too long". This checks if the lenth of the value provided is less than 2 characters or more than 50 characters and if so prompts message.

if instr(form.txtemail.value,"@") = 0 then msgbox("Invalid e-mail")
. This checks if @ is present and prompts message if not.
if (Not (IsNumeric(form.txtage.value)) then msgbox"Invalid age". This check if the value is not numeric and prompts message if so. To check if it's numeric, do not specify NOT before IsNumeric.
form.txtage.focus
. This put focus in the text box, age.
form.txtage.select
. This highlights the text in the text box.

The following example is form that validates provided information before it summits. See forms to learning how to do forms.
<html>
<head>
<script language = "vbscript">
sub cmdUserInfo_OnClick()
if len(form.txtName.value)<2 or IsNumeric(form.txtName.value)then
   msgbox("Type a valid name")
   form.txtName.focus
   form.txtName.select
elseif len(form.txtAge.value) <2 or Not IsNumeric(form.txtAge.value) then
   msgbox("Type a valid age")
   form.txtAge.focus
   form.txtAge.select
elseif form.txtEmail.value="" then
   msgbox("Enter a valid email")
   form.txtEmail.focus
   form.txtEmail.select
elseif instr(form.txtEmail.value,"@") = 0 then
   msgbox("Type a valid email")
   form.txtEmail.focus
else
   msgbox"Success!" end if
end sub
</script>
</head>
<BODY>
<form name = "form" method = "post">
<table border="1">
<tr>
  <td>Name:</td><td><input type = "text"name = "txtName"></td>
</tr>
<tr>
 <td>Age:</td><td><input type = "text" name = "txtAge" size="2"></td>
</tr>
<tr>
 <td>Email:</td><td><input type = "text"name ="txtEmail"></td>
</tr>
<tr>
 <td><input type = "button" name = "cmdUserInfo" value = "Summit"></td>
</tr>
</table>
</form>
</body>
</html>
Notice we did not call the sub procedure onclick() method. We name the command button cmdUserInfo which is the name of the sub procedure that has the validation code. This is an event driven procedure that evaluates it's code when the command button is clicked. When the button is clicked, if statement checks the name field to make sure 2 characters or more is entered and the values are not numbers. If the name field is ok, then we do the age field same making sure that two numbers are entered. If the name, and age field are ok, then we check the email field to make sure that the @ sign is present. We prompt a proper error message each time that the condition is not true.

We use table to make the form objects look friendly. The txt added to the each text field name and cmd added to the button are standard naming convention for visual basic.
Here is the result of this example

Name:
Age:
Email:

Operators MsgBox, Prompt, Confirm boxes
Listed @ ConsumerVote.com - The Consumer Rated Web Directory
PC Articles
  Computer Safety
prevent viruses & Spyware