Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 27th, 2004, 09:26 PM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validate runtime generated text field

Hi guys,
I've tried to create simple ecomm website for my school assignment. The catalogue page must be dynamic from the database. Here is some part of my code.

This code is to set up the connection and recordset
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.ConnectionString = "Provider = SQLOLEDB.1; Persist Security Info = False;Initial Catalog = Mouse;USER ID= blah; Password = blah ; Data Source = blackbeauty;"
Conn.open

Set rs = Server.CreateObject("ADODB.RecordSet")
strSQL = "SELECT * FROM Product"
rs.Open strSQL,Conn,3
%>

Here is the code that will be generated in runtime
<%
do while not rs.EOF
if (rs.Fields("stock") > 0) Then
%>

<TR>
<TD width="110" rowSpan ="2">
<IMG SRC=<%=Trim(CStr(rs.Fields("imageName")))%> ALT="This is the image" width="100" height="75">
</TD>
<TD width="120" >
<%Response.Write(rs.Fields("productID"))%>
</TD>
<TD width="500">
<%Response.Write(rs.Fields("productName"))%>
</TD>
<TD width="100" align ="center">
<%Response.Write(rs.Fields("stock"))%>
</TD>
<TD width="80" align ="center">
<%Response.Write(rs.Fields("price"))%>
</TD>
<TD width="100" align ="center">
<%Response.Write(rs.Fields("discountRate"))%>
</TD>
<TD width="80" align ="center">
<input type="text" maxlength="2" size="2" name = "order">
</TD>
</TR>

<%
end if
rs.MoveNext
loop
%>

Then I have a input type="submit" to add to shopping cart. But before go to shopping cart, I want to validate all of order number (it should be positive integer and can not be bigger than the stock). Does anybody here know how to do it ? since the input type name is always "order".

Reply With Quote
  #2 (permalink)  
Old September 27th, 2004, 10:00 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

BTW: Form validation is best done on the client side for efficiency.
(better suited in a javaScipt forum, however..)

TIP: Put the call to the function in your <form> tag instead of your submit or button tag, this way the form can be submitted by pressing enter OR Clicking:

<form name="someName" action="someAction" method="post" onSubmit="return validate();">

NOTE:Place the following inside script tags after the <head> and before the </head>:


function validate()
{
   //To ensure a value is a integer
   if(isNaN(parseInt(document.formName.orderNumber.va lue)))
   {
      alert("please enter a valid positive integer");
      //focus the incorrect field
      document.formName.orderNumber.focus();
      //select the incorrect text
      document.formName.orderNumber.select();
      return(false);
   }

   //is order number bigger than stock
   if(document.formName.orderNumber.value > document.formName.stock.value )
   {
      alert("The order number is bigger than the stock value");
      document.formName.orderNumber.focus();
      document.formName.orderNumber.select();
      return(false);
   }

   return(true);
}

JS is case and space sensitive remember

Wind is your friend
Matt
Reply With Quote
  #3 (permalink)  
Old September 27th, 2004, 11:03 PM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Matt,

There are some questions that i need to ask :
1. Do I need to add onSubmit = "return validate();" in the form tag ?
Since I have my code:

<input type ="submit" onclick = "return validate()" value="Add To Chart" id=add name=add>


2. Are you sure this validation function will be applied to all of the order text fields ? Cause I only put simple code to function validation(), like below

function validate()
{
    alert(document.catalogForm.order.value);
}

The result, there will be only 2 times of pop up window saying :"undefined". Although there are 5 items in my catalogue. I believe the two times pop up window is caused by calling validate function 2 times in form tag and submit tag.


Reply With Quote
  #4 (permalink)  
Old September 28th, 2004, 12:10 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Cochin, Kerala, India.
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

1. There is a small difference between putting the validation in the onSubmit event of form and onclick event of submit button.

If you put it in the onClick event of submit button, this will not invoked when users just press carriage when they are in one of the textboxes. So the inputs will not be validated always.

If you put in in onSubmit event of form, the form will be validated always.

Ofcourse, you need not put it in both the places.

2. Do you have a control named order in a form named catalogForm in the document ? If yes, it will not produce that error. Please note that JS is case sensitive. catalogform and catalogForm are different.
Reply With Quote
  #5 (permalink)  
Old September 28th, 2004, 12:22 AM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figured out how to validate it.
here is the code

function validate()
{
    for(i=0; i < document.catalogForm.order.length ; i++)
    {
        if(!isPositiveInteger(document.catalogForm.order[i].value))
    {
        alert("There is an invalid number of order")
        return false
    }
    }
    return true
}

Anyway Thanks madhukp and mat41 :)

Reply With Quote
  #6 (permalink)  
Old September 30th, 2004, 09:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Did you search on this on p2p.wrox site, before posting here, I remember this was handled already in another post. Glad that you figured out atlast.

Cheers!

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Text(Sql Server Text Field) to Image(JPG) srinivas72 ADO.NET 2 February 13th, 2009 06:31 PM
Runtime Column Header Text vinod_yadav1919 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 November 29th, 2008 01:53 AM
how can validate dynamic textbox field & get data alpi Classic ASP Basics 0 November 4th, 2007 03:24 PM
Validate a Text Field zaeem VB Databases Basics 0 May 20th, 2005 01:58 AM
Validate calculated field in sub form MG76 Access 4 March 18th, 2004 04:26 PM



All times are GMT -4. The time now is 07:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.