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 IamaSherpa 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 vbscript does not work. Why?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi guys,
I have an ASP application. I'd like the program to display a message if someone has not entered information in the required fields. The vbscript code does not work. I simply do not get any message display. Here is the code:

<%@ LANGUAGE = &quot;VBSCRIPT&quot;%>
<%OPTION EXPLICIT%>

<html>
<head>
<title>Directory-Panama</title>
</head>
<body bgcolor=&quot;#FFFFFF&quot; background=&quot;background.gif&quot; text=&quot;#000066&quot;>

<!--#INCLUDE file = &quot;adovbs.inc&quot;-->
<!--#INCLUDE file = &quot;dbconx.asp&quot;-->

<form action=&quot;addpersonexe.asp&quot; name=addpersonform method=&quot;post&quot;>
<table width=&quot;719&quot; border=&quot;0&quot; cellspacing=&quot;2&quot; cellpadding=&quot;0&quot; height=&quot;404&quot; align=&quot;left&quot;>
<tr>
<td valign=&quot;top&quot; width=&quot;148&quot; height=&quot;117&quot;><img src=&quot;spacer.gif&quot; width=&quot;147&quot; height=&quot;11&quot;></td>
<td height=&quot;117&quot; align=&quot;left&quot; valign=&quot;top&quot; colspan=&quot;5&quot; width=&quot;550&quot;><font size=&quot;5&quot; face=&quot;Times New Roman, Times, serif&quot; color=&quot;#990000&quot;><b><img src=&quot;title.gif&quot; width=&quot;300&quot; height=&quot;63&quot;></b></font>
<font face=&quot;Arial, Helvetica, sans-serif&quot; color =&quot;#CC0000&quot; size=&quot;2&quot;><br>
<b>Add a Person</b><hr size=&quot;0&quot; noshade></font>
<p><font size=&quot;2&quot; face=&quot;Times New Roman, Times, serif&quot; color=&quot;#000080&quot;>Fill
out the fields below to enter your information in the database. The
</font><font size=&quot;2&quot; face=&quot;Times New Roman, Times, serif&quot; color=&quot;#FF0000&quot;> *
</font><font size=&quot;2&quot; face=&quot;Times New Roman, Times, serif&quot; color=&quot;#000080&quot;> indicates required information.<br>
<br>
</font></td>
</tr>
<tr>
<td width=&quot;148&quot; height=&quot;40&quot;> </td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;40&quot; width=&quot;257&quot;><b>Last
Name <font color=&quot;#FF0000&quot;>*</font><br>
<input name=&quot;lname&quot; size=&quot;20&quot; >
</b></td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;40&quot; width=&quot;9&quot;> </td>
<td width=&quot;132&quot;><b>First Name <font color=&quot;#FF0000&quot;>*</font> <br>
<input name=&quot;fname&quot; size=&quot;15&quot; >
</b>
<td height=&quot;40&quot; width=&quot;46&quot;><b>MI</b> <br>
<input name=&quot;mi&quot; size=&quot;1&quot;>
</td>
<td height=&quot;40&quot; align=&quot;left&quot; valign=&quot;top&quot; width=&quot;90&quot;>
</td>
<td width=&quot;7&quot;>
<br>
</tr>
<tr>
<td width=&quot;148&quot; height=&quot;36&quot;> </td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;36&quot; width=&quot;257&quot;><b> Street
Address<br>
<input name=&quot;streetname&quot; size=&quot;25&quot;>
</b></td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;36&quot; width=&quot;9&quot;> </td>
<td width=&quot;132&quot; cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;36&quot;><b>City<br>
<input name=&quot;city&quot; size=&quot;15&quot;>
</b></td>
<td width=&quot;46&quot; valign=&quot;top&quot; align=&quot;left&quot;><b>State</b> <br>
<input name=&quot;state&quot; size=&quot;1&quot;>
</td>
<td align=&quot;left&quot; cellspacing=&quot;0&quot; valign=&quot;top&quot; height=&quot;36&quot; width=&quot;90&quot;><b>Zip
Code<br>
<input name=&quot;zipcode&quot; size=&quot;5&quot;>
</b></td>
</tr>
<tr>
<td width=&quot;148&quot; height=&quot;52&quot;> </td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;52&quot; width=&quot;257&quot; nowrap><b>Postal
address </b><font size=&quot;2&quot;> <font size=&quot;1&quot;> </font></font><b><br>
<input name=&quot;postal_address&quot; size=&quot;30&quot;>
<br>
</b></td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;52&quot; width=&quot;9&quot; nowrap><b>
</b></td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;52&quot; colspan=&quot;3&quot; width=&quot;276&quot;><b>Country
<font color=&quot;#FF0000&quot;>*</font></b><br>
<%
Dim myRS, country
Response.Write &quot;<select name = 'country'>&quot;
Response.Write &quot;<option value=''>Choose a Country</option>&quot;
Set myRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)
myRS.Open &quot;SELECT * FROM country order by country&quot;, myConn
While Not myRS.EOF
country = myRS(&quot;country&quot;)
Response.Write &quot;<option value = '&quot; & country & &quot;'>&quot;
Response.Write country & &quot;</option>&quot;
myRS.MoveNext
Wend
Response.Write &quot;</select>&quot;
myRS.Close
%>
</td>
</tr>
<tr>
<td width=&quot;148&quot; height=&quot;49&quot;> </td>
<td width=&quot;257&quot; cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;49&quot;><b>Primary
telephone number<br>
<input name=&quot;telephone1&quot;>
</b></td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;49&quot; width=&quot;9&quot;>
<p align=&quot;left&quot;> </p>
</td>
<td cellspace=&quot;0&quot; valign=&quot;top&quot; align=&quot;left&quot; height=&quot;49&quot; colspan=&quot;3&quot; width=&quot;276&quot;><b>Alternate
telephone number<br>
<input name=&quot;telephone2&quot;>
</b></td>
</tr>
<tr>
<td width=&quot;148&quot;> </td>
<td width=&quot;257&quot; align=&quot;left&quot; valign=&quot;top&quot;><b>Primary email <font color=&quot;#FF0000&quot;>*</font><br>
<input name=&quot;email1&quot;>
</b>
<td width=&quot;9&quot; align=&quot;left&quot; valign=&quot;top&quot;> 
<td colspan=&quot;3&quot; align=&quot;left&quot; valign=&quot;top&quot; width=&quot;276&quot;><b>Alternate email<br>
<input name=&quot;email2&quot;>
</b>
<tr>
<td width=&quot;148&quot;> </td>
<td colspan=&quot;5&quot; align=&quot;left&quot; valign=&quot;top&quot; width=&quot;550&quot;><b><br>
Website</b><br>
<input name=&quot;website&quot; size=&quot;35&quot;>
<tr>
<td width=&quot;148&quot;> </td>
<td width=&quot;257&quot; align=&quot;left&quot; valign=&quot;top&quot;><b>
Type of Business:<font color=&quot;#FF0000&quot;>*</font></b><br>
<%
Dim btype
Response.Write &quot;<select name = 'type'>&quot;
Response.Write &quot;<option value=''>Choose a Business Category</option>&quot;
Set myRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)
myRS.Open &quot;SELECT * FROM worktype order by type&quot;, myConn
While Not myRS.EOF
btype = myRS(&quot;type&quot;)
Response.Write &quot;<option value = '&quot; & btype & &quot;'>&quot;
Response.Write btype & &quot;</option>&quot;
myRS.MoveNext
Wend
Response.Write &quot;</select>&quot;
myRS.Close
Set myRS= Nothing
myconn.Close
Set myConn= Nothing
%>
</td>
<td width=&quot;9&quot; align=&quot;left&quot; valign=&quot;top&quot;> </td>
<td colspan=&quot;4&quot; align=&quot;left&quot; valign=&quot;top&quot; width=&quot;287&quot;><b>
Describe your type of business:<br>
<textarea name=&quot;description&quot; cols=&quot;20&quot; rows=&quot;1&quot;></textarea>
</b>
</tr>
<tr>
<td width=&quot;148&quot;> </td>
<td width=&quot;257&quot; align=&quot;right&quot; valign=&quot;top&quot;> <br>
<input type=&quot;submit&quot; value=&quot;Add entry&quot; name=&quot;Submit&quot;>
<td width=&quot;9&quot; align=&quot;right&quot; valign=&quot;top&quot;> 
<td colspan=&quot;4&quot; align=&quot;left&quot; valign=&quot;top&quot; width=&quot;287&quot;> <br>
<input type=&quot;reset&quot; value=&quot;Clear&quot;>
</tr>
<script language=VBScript>
Sub btnSubmit_OnClick()
If Len (frmaddpersonform.txtlname.value) = 0 then
Alert &quot;You must enter your last name&quot;
addpersonform.lname.focus
Exit sub
ElseIf Len (frmaddpersonform.txtfname.value) = 0 then
Alert &quot;You must enter your first name&quot;
addpersonform.fname.focus
Exit Sub
End if
Call frmaddpersonform.submit()
end sub
</script>


<tr>
<td width=&quot;148&quot;> </td>
<td colspan=&quot;6&quot; align=&quot;left&quot; valign=&quot;top&quot; width=&quot;561&quot;>
<hr noshade size=&quot;0&quot; align=&quot;center&quot;>
<font size=&quot;1&quot;><a href=&quot;index.html&quot;>Home</a>|<a href=&quot;searchpersonform.asp&quot;>People Search</a>| Organization/Business Search |<b>Add
personal record</b>| Add business/organization record</font> <br>
<hr size=&quot;0&quot; noshade align=&quot;center&quot;>
<a href=&quot;mailto:itzel@mail.utexas.edu&quot;><font size=&quot;2&quot;>itzel@mail.utexas.edu</font></a><font size=&quot;2&quot;><br>
<font size=&quot;1&quot;>University of Texas<br>
Graduate School of Library and Information Science</font></font></td>
</tr>
</table>
</cfform>
</body>
</html>
</script>
 
I changed a few things around -- First, I changed your submit button to a regular old button -- and gave it an onClick event of the function (which I also renamed for my own preference)...

Then, you were asking for frmaddpersonform -- or something like that, when your actual form name was addpersonform, so that's all you should have asked for... same with your text boxes. They were named fname and lname, but you were asking for txtlname, and txtfname (Sounds like you have been doing some vb, but even there, you have to name your stuff whatever you want to refer to them by) --

Anywho, without further ado, here's what I came up with and tested. Worked fine for me.

...
Code:
      <input type=&quot;button&quot; value=&quot;Add entry&quot; name=&quot;Submit&quot; onClick=&quot;submitThis()&quot;>
    <td width=&quot;9&quot; align=&quot;right&quot; valign=&quot;top&quot;>  
    <td colspan=&quot;4&quot; align=&quot;left&quot; valign=&quot;top&quot; width=&quot;287&quot;> <br>
      <input type=&quot;reset&quot; value=&quot;Clear&quot;>
  </tr>
<script language=VBScript>
Sub submitThis()
If addpersonform.lname.value = &quot;&quot; then
alert &quot;You must enter your last name&quot;
addpersonform.lname.focus
Exit sub
ElseIf Len (addpersonform.fname.value) = 0 then
Alert &quot;You must enter your first name&quot;
addpersonform.fname.focus
Exit Sub
End if
Call addpersonform.submit()
end sub
</script>

I only posted the portion of the code that I changed to get it to work.

good luck! :)
Paul Prewett
 
Just as a note, I'm not sure that the Call addpersonform.submit() is going to do the trick --

I know it works in javascript, but I don't use client side vbscript -- so I'm not sure about it...

Maybe someone else can confirm or deny that.

:)
 
It's worth noting that Netscape does not support client side VBScript (unless you go an purchase a third party add-on). So unless your users are exclusively IE, it's probably a better idea to use Javascript for the client side stuff.

Unfortunately, I found this out the hard way after coding all the client side in VBScript before testing in Netscape. Mise Le Meas,

Mighty :)
 
link 9 (since you rewrote this code), or anyone...

I was scanning this thread, and I am doing something similar to what mcclareni is doing.

I follow the logic of your code example above for form field validation and it does work for me. However a funny thing keeps happening. The code catches validations (like if a field is left blank)gives me my appropriate error message, but then still processes the form and brings me to my asp page. I'm looking for something like a &quot;cancel event&quot; command somewhere that would stop the browswer from executing the action in the form tag.

An analogy in the previous code example would be if
1. you left lname field blank
2. the code generated an alert &quot;You must enter your last name&quot;
3. and instead of canceling the all processsing and forcing the user back to that field on AddPersonForm, AddPersonExe.asp runs anyway

I tried putting in something like:

frmAddPersonForm_OnSubmit = False

but that didn't seem to work.

Any ideas?

PS - I copied the code below (from this example)for easier reference.


<form action=&quot;addpersonexe.asp&quot; name=addpersonform method=&quot;post&quot;>


<input type=&quot;button&quot; value=&quot;Add entry&quot; name=&quot;Submit&quot; onClick=&quot;submitThis()&quot;>
<td width=&quot;9&quot; align=&quot;right&quot; valign=&quot;top&quot;>
<td colspan=&quot;4&quot; align=&quot;left&quot; valign=&quot;top&quot; width=&quot;287&quot;> <br>
<input type=&quot;reset&quot; value=&quot;Clear&quot;>
</tr>
<script language=VBScript>
Sub submitThis()
If addpersonform.lname.value = &quot;&quot; then
alert &quot;You must enter your last name&quot;
addpersonform.lname.focus
Exit sub
ElseIf Len (addpersonform.fname.value) = 0 then
Alert &quot;You must enter your first name&quot;
addpersonform.fname.focus
Exit Sub
End if
Call addpersonform.submit()
end sub
</script>

 
Well, like I said in the post up there, I wasn't sure that the formName.submit() would work in vbscript -- and as it turns out, it doesn't --

To be honest, I would do form validation with client side javascript such as what we were talking about over in the ASP forum earlier today.

The reason I say that is that client side vbscript is proprietary to Internet Explorer, so anyone looking at your page in Nutscrape would not be able to take advantage of the form validation -- and futhermore, if you submit the form with the function, it wouldn't even be submitted by NS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top