Solved

asp form Validation

Posted on 2013-01-08
10
384 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now