Improve company productivity with a Business Account.Sign Up

x
?
Solved

asp form Validation

Posted on 2013-01-08
10
Medium Priority
?
430 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 180 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 300 total points
ID: 38754168
Why not use the RequiredFieldValidator control?

http://msdn.microsoft.com/en-us/library/5hbw267h(v=vs.100).aspx
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 18

Accepted Solution

by:
nap0leon earned 720 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 720 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:Obadiah Christopher
Obadiah Christopher earned 300 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 720 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 720 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…

607 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