Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

asp form Validation

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
genewc
Asked:
genewc
7 Solutions
 
wdfdo1986Commented:
Whats the problem with it? What do you want us to do? Please be more specific.
0
 
genewcAuthor Commented:
need to have a pop up to validation as the user input the info in, for the user can't leave it blank
0
 
ingriTCommented:
Why not use the RequiredFieldValidator control?

http://msdn.microsoft.com/en-us/library/5hbw267h(v=vs.100).aspx
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
nap0leonCommented:
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
 
genewcAuthor Commented:
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
 
nap0leonCommented:
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
 
informaniacCommented:
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
 
nap0leonCommented:
@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
 
nap0leonCommented:
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
 
genewcAuthor Commented:
found the problem thru google
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now