Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation using VB Script

Status
Not open for further replies.

exphtur

Technical User
Jul 23, 2003
23
IE
I am having a problem with validation, I have a new members registration form which works at calling up the second form and adding the details to my Microsoft Access database,  the problem I have is that I want to validate it using VB Script for missing name address number etc but I can't seem to get this to work.  The code from the two forms are below.  Any help would be very much appreciated.  Thank you
This is the new member registration form:
<form name="form" method="post" action="addnewmember2.asp">
<table width="80%" border="0">
  <tr>
    <td width="63%"><div align="right"><font face="Arial, Helvetica, sans-serif">First Name:</font></div></td>
    <td width="37%"><input name="txtName" type="text" size="15" maxlength="30"></td>
  </tr>
  <tr>
    <td height="28"><div align="right"><font face="Arial, Helvetica, sans-serif">Last Name:</font></div></td>
    <td><input name="txtName2" type="text" size="15" maxlength="20"></td>
  </tr>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Address1:</font></div></td>
    <td><input name="txtAddress1" type="text" size="24"></td>
  </tr>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Address2:</font></div></td>
    <td><input type="text" name="txtAddress2"></td>
  </tr>
  <tr>
    <td height="27"><div align="right"><font face="Arial, Helvetica, sans-serif">Date of
    Birth:</font></div></td>
    <td><input name="txtDOB" type="number" size="10" maxlength="12"></td>
  </tr>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Telephone
    No:</font></div></td>
    <td><input name="txtTelephone" type="number" size="15"></td>
  </tr>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Mobile No:</font></div></td>
    <td><input name="txtMobile" type="number" size="15"></td>
  </tr>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Email :</font></div></td>
    <td><input type="text" name="txtEmail"></td>
  </tr>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">UserName:</font></div></td>
    <td><input name="txtUserName" type="text" size="15"></td>
  </tr>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Password:</font></div></td>
    <td><input name="txtPassword" type="text" size="15"></td>
  </tr>
  <tr>
  <td><div align="right"> <input type="submit" name="Submit" value="Submit"><input type="reset" name="reset" value="Reset"></div></td>
  </tr>
</table>
  </form>
This is the second form
<%
dim conn, rs, sql, intTelephone, intMobile
set rs = Server.CreateObject ("ADODB.Recordset")
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\videostore\videostore.mdb;Persist Security Info=False"
sql = "SELECT * FROM [tblMember]"
rs.open sql, conn, 3, 3
strFName= Trim(Request.Form("txtName"))
strLName=Trim(Request.Form("txtName2"))
strAddress1=Trim(Request.Form("txtAddress1"))
strAddress2=Trim(Request.Form("txtAddress2"))
strDOB=Trim(Request.Form("txtDOB"))
intTelephone=Trim(Request.Form("txtTelephone"))
intMobile=Trim(Request.Form("txtMobile"))
strEmail=Trim(Request.Form("txtEmail"))
strUserName=Trim(Request.Form("txtUserName"))
strPassword=Trim(Request.Form("txtPassword"))
rs.AddNew
    rs("FName")=strFName
    rs("LName")=strLName
    rs("Address1")=strAddress1
    rs("Address2")=strAddress2
    rs("DOB")=strDOB
    rs("Phone")=intTelephone
    rs("Mobile")=intMobile
    rs("Email")=strEmail
    rs("UserName")=strUserName
    rs("Password")=strPassword
    
rs.Update
rs.close
Response.Redirect "login.asp"
%>
 
If your looking for the correct place to put validation, the most minimal aproach would be to add it before you open your connection to the databae. i would move the variables tat are gettong their values from the form inputs above the connection opening portion of te code, then validate them there. Perhaps something like this:
Code:
<%
dim conn, rs, sql, intTelephone, intMobile

'get the values from form collection
strFName= Trim(Request.Form("txtName"))
strLName=Trim(Request.Form("txtName2"))
strAddress1=Trim(Request.Form("txtAddress1"))
strAddress2=Trim(Request.Form("txtAddress2"))
strDOB=Trim(Request.Form("txtDOB"))
intTelephone=Trim(Request.Form("txtTelephone"))
intMobile=Trim(Request.Form("txtMobile"))
strEmail=Trim(Request.Form("txtEmail"))
strUserName=Trim(Request.Form("txtUserName"))
strPassword=Trim(Request.Form("txtPassword"))

'validate the values - simple validation
If Len(strFName) = 0 Or Len(StrLName) = 0 Or len(strAddress1) = 0 Or Not isDate(strDOB) Or len(intTelephone) <> 10 or len(intMobile) <> 10 Or len(strEmail) = 0 Or len(strUserName) = 0 or len(strPassword) = 0 Then
   'add in an error messge here
   Response.Write "Please return to the form on the previous page and make sure you fill out all entries."
   
   'send nothing more
   Response.End

End If

'continue on
set rs = Server.CreateObject ("ADODB.Recordset")
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\videostore\videostore.mdb;Persist Security Info=False"
sql = "SELECT * FROM [tblMember]"
rs.open sql, conn, 3, 3

Now that is a rather simple check. If I were doing this I would use something similar to the simple check and instead output the form again, then add nice little highlights or red stars where tey need to fix things. This requires larger changes to the second file, but may be worth it.
Code:
<%
dim conn, rs, sql, intTelephone, intMobile

'connection ceated early for username validation
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\videostore\videostore.mdb;Persist Security Info=False"

'get the values from form collection
strFName= Trim(Request.Form("txtName"))
strLName=Trim(Request.Form("txtName2"))
strAddress1=Trim(Request.Form("txtAddress1"))
strAddress2=Trim(Request.Form("txtAddress2"))
strDOB=Trim(Request.Form("txtDOB"))
intTelephone=Trim(Request.Form("txtTelephone"))
intMobile=Trim(Request.Form("txtMobile"))
strEmail=Trim(Request.Form("txtEmail"))
strUserName=Trim(Request.Form("txtUserName"))
strPassword=Trim(Request.Form("txtPassword"))

'see if the username already exists in db - check the field name, I guessed
Set rs = conn.Execute("SELECT * FROM tblMember WHERE strUsername = '" & srUsername & "'"

'set up a regular expression for email
Dim regex
Set regex = New RegExp
regex.pattern = "^[\w-_\.]+\@[\w-_]+(\.[\w-_]+)+$"
regex.global = False

'validate the values - somewhat simple validation
'	requires >0 text for address, names
'	requires email match a regular expression pattern
'   requires username to not be in db already
'	requires telephone numbers to be 10 characters
'	requires password to be at least 8 characters
'	requires DOB to be a date (iffy, since 1/6 will pass isDate but is not a DOB)
If Len(strFName) = 0 Or Len(StrLName) = 0 Or len(strAddress1) = 0 Or Not isDate(strDOB) Or len(intTelephone) <> 10 or len(intMobile) <> 10 Or Not regex.Test(strEmail) Or Not rs.EOF or len(strPassword) < 8 Then
   're-display the form with little red comments
	%>
	<form name="form" method="post" action="addnewmember2.asp">
<table width="80%" border="0">
  <%  If len(strFName) = 0 Then Response.Write ErrorMessage("Please enter a value for First Name") %>
  <tr>
    <td width="63%"><div align="right"><font face="Arial, Helvetica, sans-serif">First Name:</font></div></td>
    <td width="37%"><input name="txtName" type="text" size="15" maxlength="30" value="<%=strFName%>"></td>
  </tr>
  <%  If len(strLName) = 0 Then Response.Write ErrorMessage("Please enter a value for Last Name") %>
  <tr>
    <td height="28"><div align="right"><font face="Arial, Helvetica, sans-serif">Last Name:</font></div></td>
    <td><input name="txtName2" type="text" size="15" maxlength="20" value="<%=strLName%>"></td>
  </tr>
  <%  If len(txtAddress1) = 0 Then Response.Write ErrorMessage("Please enter your address.") %>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Address1:</font></div></td>
    <td><input name="txtAddress1" type="text" size="24" value="<%=txtAddress1%>"></td>
  </tr>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Address2:</font></div></td>
    <td><input type="text" name="txtAddress2" value="<%=txtAddress2%>"></td>
  </tr>
  <%  If Not isDate(strDOB) Then Response.Write ErrorMessage("Please enter a vali date for Date of Birth.") %>
  <tr>
    <td height="27"><div align="right"><font face="Arial, Helvetica, sans-serif">Date of
    Birth:</font></div></td>
    <td><input name="txtDOB" type="number" size="10" maxlength="12" value="<%=strDOB%>"></td>
  </tr>
  <%  If len(intTelephone) <> 10 Then Response.Write ErrorMessage("Please enter a valid phone number, including area code.") %>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Telephone
    No:</font></div></td>
    <td><input name="txtTelephone" type="number" size="15" value="<%=intTelephone%>"></td>
  </tr>
  <%  If len(intMobile) <> 10 Then Response.Write ErrorMessage("Please enter a valid phone number, including area code.") %>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Mobile No:</font></div></td>
    <td><input name="txtMobile" type="number" size="15" value="<%=intMobile%>"></td>
  </tr>
  <%  If regex.Test(strEmail) Then Response.Write ErrorMessage("Please enter a valid email address.") %>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Email :</font></div></td>
    <td><input type="text" name="txtEmail" value="<%=strEmail%>"></td>
  </tr>
  <%  If Not rs.EOF Then Response.Write ErrorMessage("The username you have entered is already in use, please choose another.") %>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">UserName:</font></div></td>
    <td><input name="txtUserName" type="text" size="15" value="<%=strUsername%>"></td>
  </tr>
  <%  If len(strPassword) < 8 Then Response.Write ErrorMessage("Please choose a longer password, your password must be at least 8 characters long.") %>
  <tr>
    <td><div align="right"><font face="Arial, Helvetica, sans-serif">Password:</font></div></td>
    <td><input name="txtPassword" type="text" size="15" value="<%=strPassword%>"></td>
  </tr>
  <tr>
  <td><div align="right"> <input type="submit" name="Submit" value="Submit"><input type="reset" name="reset" value="Reset"></div></td>
  </tr>
</table>
  </form>
  <%
   
   'send nothing more
   Response.End

End If

'---ErrorMessage Function should be defined near bottom for better readability
Function ErrorMessage(msg)
   ErrorMessage = "<tr><td colspan=""2"" style=""background-color: #FFFFCC;"">" & msg & "</td></tr>"
 End Function

'continue on
set rs = Server.CreateObject ("ADODB.Recordset")
sql = "SELECT * FROM [tblMember]"
rs.open sql, conn, 3, 3

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top