Solved

asp form Validation

Posted on 2013-01-08
10
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validation for a field in asp.net 3 31
Check only one toolstripmenu item 12 62
RestClient.cs fails at (HttpWebResponse)request.GetResponse()) 4 53
Convert a string to date 4 40
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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