Solved

asp form Validation

Posted on 2013-01-08
10
396 Views
Last Modified: 2013-05-30
Hi
I am using a small form form for our website and need to do validation
Can someone help with the code to do this with, here is the code that I am using

<%
  if Request("submit") <> "" then
      Dim objCDO
      Set objCDO      = Server.CreateObject("CDO.Message")
      Dim objCDOConf

     Set objCDOConf  = Server.CreateObject("CDO.Configuration")
      With objCDOConf.Fields
          .Item(cdoSendUsingMethod) = 2
          .Item(cdoSMTPServer) = "" 
          .Item(cdoSMTPServerPort) = 25
          .Item(cdoSMTPconnectiontimeout) = 10
          .Update
      End With
      Set objCDO.Configuration = objCDOConf
      ' Be sure to use a valid email addresses below:
      objCDO.From     = "" 
      objCDO.To       = "" 
      objCDO.Subject  = "Website Info Page " & Request.ServerVariables("URL")
      objCDO.TextBody = "Website Info Page " & Request.ServerVariables("URL")
        
      objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
      objCDO.TextBody = objCDO.TextBody & "First Name: " 
      objCDO.TextBody = objCDO.TextBody & Request("First Name:")
        
      objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
      objCDO.TextBody = objCDO.TextBody & "Last Name: " 
      objCDO.TextBody = objCDO.TextBody & Request("Last Name:")
        
      objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
      objCDO.TextBody = objCDO.TextBody & "Email: " 
      objCDO.TextBody = objCDO.TextBody & Request("Email:")
        
      objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
        objCDO.TextBody = objCDO.TextBody & "Phone: " 
      objCDO.TextBody = objCDO.TextBody & Request("Phone:")
                 
      objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
        objCDO.TextBody = objCDO.TextBody & "City: " 
      objCDO.TextBody = objCDO.TextBody & Request("City:")
                   
      objCDO.Send
         
      'Clean-up
      Set objCDO     = Nothing  
      Set objCDOConf = Nothing  
     
      ' Happy response. If you would rather,
      ' change the following to
       'Response.Redirect "thankyou.html"
        Response.Write "Your Message has been sent. </br> Thank you!
      Response.End
      end if
%>
<html>

<head>
<link href="css/form.css" rel="stylesheet" type="text/css">
  <style type="text/css">
.style1 {
      margin-right: 0px;
}
      .newStyle1
      {
      }
  </style>
</head>

  <body>
 
  <form method=POST action="<%=Request.ServerVariables("URL")%>" style="height: 225px; width: 250px;" class="style1">
  <table width="250" border=0 align="left" cellpadding=5 cellspacing=0 style="width: 250px;">
    <tr><td width="88" nowrap="nowrap">First Name:&nbsp;</td><td width="146"><input type=textbox
          name="First Name:"></td>
        </tr>
        <tr><td nowrap="nowrap">Last Name:&nbsp;</td><td><input type=textbox
          name="Last Name:"></td>
    <asp:Textbox id="First Name:" runat="server"/>
  </TD></TR>

       
        <tr><td>Email:&nbsp;</td><td><input type=textbox
          name="Email:"></td>
        </tr>
         <tr><td>Phone:&nbsp;</td><td><input type=textbox
          name="Phone:">
          </td>
       
       
        <tr><td>City:&nbsp;</td><td><input type=textbox
          name="City:">
            </td>
                   
             
        <tr><td height="72" colspan=2><p>
          <input type=submit
          name=submit value="Request Info">
          <input name="" type="reset" value="Reset">
        </p>
           
    <p>&nbsp;</p></td></tr>
      </table>
    </form>
  </body>
</html>
0
Comment
Question by:genewc
10 Comments
 
LVL 7

Assisted Solution

by:wdfdo1986
wdfdo1986 earned 60 total points
ID: 38753840
Whats the problem with it? What do you want us to do? Please be more specific.
0
 

Author Comment

by:genewc
ID: 38753883
need to have a pop up to validation as the user input the info in, for the user can't leave it blank
0
 
LVL 6

Assisted Solution

by:ingriT
ingriT earned 100 total points
ID: 38754168
Why not use the RequiredFieldValidator control?

http://msdn.microsoft.com/en-us/library/5hbw267h(v=vs.100).aspx
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 18

Accepted Solution

by:
nap0leon earned 240 total points
ID: 38754948
First off... umm, wow.  Your page has combination of .Net and Classic ASP on it and lots of HTML errors.  I cleaned it up.  Major fix was removing the colons from teh field names.  That caused issues.

Try this:
<%
errorCount = 0
IF Request("submit") <> "" THEN
	If Request("FirstName") = "" Then errorCount = errorCount + 1 End IF
	If Request("LastName") = "" Then errorCount = errorCount + 1 End IF
	If Request("Email") = "" Then errorCount = errorCount + 1 End IF
	If Request("Phone") = "" Then errorCount = errorCount + 1 End IF
	If Request("City") = "" Then errorCount = errorCount + 1 End IF

	IF errorCount = 0 THEN
		'CODE GOES HERE TO CREATE AND SEND MESSAGE
		Response.Write ("Sending Message")
		Response.End
	ELSE
		'Don't do anything... the errorCount is > 0 so the "all fields are required message will appear
	END IF
END IF
%>
<html>

<head>
<link href="css/form.css" rel="stylesheet" type="text/css">
<style type="text/css">
.style1 {
      margin-right: 0px;
}
.newStyle1 {
}
  </style>
</head>

<body>
 
	<form method=GET action="<%=Request.ServerVariables("URL")%>" style="height: 225px; width: 250px;" class="style1">
		<table width="250" border=0 align="left" cellpadding=5 cellspacing=0 style="width: 250px;">
		<% IF errorCount > 0 THEN %>
		<tr>
			<td colspan=2><b>All fields are required!</b></td>
		</tr>
		<% END IF %>
		<tr>
			<td width="88" nowrap="nowrap">First Name:&nbsp;</td>
			<td width="146"><input type=textbox name="FirstName"></td>
		</tr>
		<tr>
			<td nowrap="nowrap">Last Name:&nbsp;</td>
			<td><input type=textbox name="LastName"></td>
		</tr>
		<tr>
			<td>Email:&nbsp;</td>
			<td><input type=textbox name="Email"></td>
		</tr>
		<tr>
			<td>Phone:&nbsp;</td>
			<td><input type=textbox name="Phone"></td>
		</tr>
		<tr>
			<td>City:&nbsp;</td>
			<td><input type=textbox name="City"></td>
		</tr>
		<tr>
			<td height="72" colspan=2>
				<p>
					<input type=submit name=submit value="Request Info">
					<input name="" type="reset" value="Reset">
				</p>
				<p>&nbsp;
				</p>
			</td>
		</tr>
		</table>
	</form>
</body>
</html> 

Open in new window

0
 

Author Comment

by:genewc
ID: 38755814
Thank You it does work but missing the code for the Email and SMTP server
Can you please with that to
This is runing on IIS 7
0
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 240 total points
ID: 38755981
I removed the code for the email because I don't have email on my system to verify that what you already had there was valid.

It goes into lines 11-13.
Presuming what you had before in the Classic ASP section is correct, you will only need to change these lines:
Request("First Name:") 

Open in new window

to like this:
Request("FirstName")

Open in new window

0
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 100 total points
ID: 38762530
Not very sure, but can't you have client-side validation before the server-side validation?
<%
errorCount = 0
IF Request("submit") <> "" THEN
	If Request("FirstName") = "" Then errorCount = errorCount + 1 End IF
	If Request("LastName") = "" Then errorCount = errorCount + 1 End IF
	If Request("Email") = "" Then errorCount = errorCount + 1 End IF
	If Request("Phone") = "" Then errorCount = errorCount + 1 End IF
	If Request("City") = "" Then errorCount = errorCount + 1 End IF

	IF errorCount = 0 THEN
		'CODE GOES HERE TO CREATE AND SEND MESSAGE
		Response.Write ("Sending Message")
		Response.End
	ELSE
		'Don't do anything... the errorCount is > 0 so the "all fields are required message will appear
	END IF
END IF
%>
<html>

<head>
<link href="css/form.css" rel="stylesheet" type="text/css">
<style type="text/css">
.style1 {
      margin-right: 0px;
}
.newStyle1 {
}
  </style>
<script type="text/javascript" language="javascript">
function ValidateInput()
{
         var msg="";
         if(document.getElementByID("FirstName").value == "")
         {
               msg += "Please enter First Name\n";
         }
         if(document.getElementByID("LastName").value == "")
         {
               msg += "Please enter Last Name";
         }
         if(msg!="")
         {
               alert(msg);
               return false;
         }
         return true;
}
</script>
</head>

<body>
 
	<form method=GET action="<%=Request.ServerVariables("URL")%>" style="height: 225px; width: 250px;" class="style1">
		<table width="250" border=0 align="left" cellpadding=5 cellspacing=0 style="width: 250px;">
		<% IF errorCount > 0 THEN %>
		<tr>
			<td colspan=2><b>All fields are required!</b></td>
		</tr>
		<% END IF %>
		<tr>
			<td width="88" nowrap="nowrap">First Name:&nbsp;</td>
			<td width="146"><input type=textbox name="FirstName" id="FirstName"></td>
		</tr>
		<tr>
			<td nowrap="nowrap">Last Name:&nbsp;</td>
			<td><input type=textbox name="LastName" id="LastName"></td>
		</tr>
		<tr>
			<td>Email:&nbsp;</td>
			<td><input type=textbox name="Email"></td>
		</tr>
		<tr>
			<td>Phone:&nbsp;</td>
			<td><input type=textbox name="Phone"></td>
		</tr>
		<tr>
			<td>City:&nbsp;</td>
			<td><input type=textbox name="City"></td>
		</tr>
		<tr>
			<td height="72" colspan=2>
				<p>
					<input type=submit name=submit value="Request Info" onclick="javascript:return ValidateInput();">
					<input name="" type="reset" value="Reset">
				</p>
				<p>&nbsp;
				</p>
			</td>
		</tr>
		</table>
	</form>
</body>
</html> 

Open in new window

0
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 240 total points
ID: 38763296
@informaniac - absolutely!

There are a variety of styles to running client-side validation.
The example you posted being one of the most common "beginner" approaches to it (alert the errors).
Nicer looking methods would include styling the error fields with alternate colors to indicate an error and having the error message appear with the field in question.  These errors could be thrown whenever a user exits a field ("on-the-fly") or when the "Submit" button is clicked (or both!).
In all cases, you will still want to perform server-side validation too... Any medium-grade developer/hacker can fake a form submission from their local PC that bypasses any client-side validation you put into your page.
0
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 240 total points
ID: 38763368
Here is a version with the Email stuff in place.
<%
errorCount = 0
IF Request("submit") <> "" THEN
	If Request("FirstName") = "" Then errorCount = errorCount + 1 End IF
	If Request("LastName") = "" Then errorCount = errorCount + 1 End IF
	If Request("Email") = "" Then errorCount = errorCount + 1 End IF
	If Request("Phone") = "" Then errorCount = errorCount + 1 End IF
	If Request("City") = "" Then errorCount = errorCount + 1 End IF

	IF errorCount = 0 THEN
		'CODE GOES HERE TO CREATE AND SEND MESSAGE
		ExecuteBlockOfCode = False  //Dummy Variable so this runs on my machine.  Change to TRUE or remove entirely
		IF ExecuteBlockOfCode THEN
		Dim objCDO
		Set objCDO      = Server.CreateObject("CDO.Message")
		Dim objCDOConf

		Set objCDOConf  = Server.CreateObject("CDO.Configuration")
		With objCDOConf.Fields
			.Item(cdoSendUsingMethod) = 2
			.Item(cdoSMTPServer) = ""
			.Item(cdoSMTPServerPort) = 25
			.Item(cdoSMTPconnectiontimeout) = 10
			.Update
		End With

		Set objCDO.Configuration = objCDOConf
		' Be sure to use a valid email addresses below:
			objCDO.From     = ""
			objCDO.To       = ""  
			objCDO.Subject  = "Website Info Page " & Request.ServerVariables("URL")
			objCDO.TextBody = "Website Info Page " & Request.ServerVariables("URL")
        
			objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
			objCDO.TextBody = objCDO.TextBody & "First Name: "
			objCDO.TextBody = objCDO.TextBody & Request("FirstName")
        
			objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
			objCDO.TextBody = objCDO.TextBody & "Last Name: "
			objCDO.TextBody = objCDO.TextBody & Request("LastName")
        
			objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
			objCDO.TextBody = objCDO.TextBody & "Email: "
			objCDO.TextBody = objCDO.TextBody & Request("Email")
        
			objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
			objCDO.TextBody = objCDO.TextBody & "Phone: "
			objCDO.TextBody = objCDO.TextBody & Request("Phone")
                 
			objCDO.TextBody = objCDO.TextBody & chr(10) & chr(13)
			objCDO.TextBody = objCDO.TextBody & "City: "
			objCDO.TextBody = objCDO.TextBody & Request("City")
                   
			objCDO.Send
         
		'Clean-up
		Set objCDO     = Nothing  
		Set objCDOConf = Nothing  
		End IF
 
 		' Happy response. If you would rather,
		' change the following to	
		'Response.Redirect "thankyou.html"
	        Response.Write ("Your Message has been sent. </br> Thank you!")
		Response.End

	ELSE
		'Don't do anything... the errorCount is > 0 so the "all fields are required message will appear
	END IF
END IF
%>
<html>

<head>
<link href="css/form.css" rel="stylesheet" type="text/css">
<style type="text/css">
.style1 {
      margin-right: 0px;
}
.newStyle1 {
}
  </style>
</head>

<body>
 
	<form method=GET action="<%=Request.ServerVariables("URL")%>" style="height: 225px; width: 250px;" class="style1">
		<table width="250" border=0 align="left" cellpadding=5 cellspacing=0 style="width: 250px;">
		<% IF errorCount > 0 THEN %>
		<tr>
			<td colspan=2><b>All fields are required!</b></td>
		</tr>
		<% END IF %>
		<tr>
			<td width="88" nowrap="nowrap">First Name:&nbsp;</td>
			<td width="146"><input type=textbox name="FirstName"></td>
		</tr>
		<tr>
			<td nowrap="nowrap">Last Name:&nbsp;</td>
			<td><input type=textbox name="LastName"></td>
		</tr>
		<tr>
			<td>Email:&nbsp;</td>
			<td><input type=textbox name="Email"></td>
		</tr>
		<tr>
			<td>Phone:&nbsp;</td>
			<td><input type=textbox name="Phone"></td>
		</tr>
		<tr>
			<td>City:&nbsp;</td>
			<td><input type=textbox name="City"></td>
		</tr>
		<tr>
			<td height="72" colspan=2>
				<p>
					<input type=submit name=submit value="Request Info">
					<input name="" type="reset" value="Reset">
				</p>
				<p>&nbsp;
				</p>
			</td>
		</tr>
		</table>
	</form>
</body>
</html> 

Open in new window


Note: you will need to change line 12 to TRUE or remove the If-End If around the CDO stuff (lines 12 and 59) for it to execute the actual email-creation section.
0
 

Author Closing Comment

by:genewc
ID: 39207841
found the problem thru google
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error handling in asp.net site 5 29
Button to go back 3 25
VB.NET 2008 - SQL Timeout 9 25
Hide cell in a table 2 15
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question