473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Validate data during entry in table fields against database in ASP classic

I have a form with various fields, one of which I need to check against data in a database table. i.e if the value entered into the text input field exists in the database, the form should be submitted normally; otherwise an error message should be issued without submitting the form. How can I achieve this kind of validation?

Thanks
Jun 30 '09 #1
8 11738
Frinavale
9,735 Expert Mod 8TB
You should always do server side validation.

You can also use JavaScript to do some client side validation to prevent unnecessary, invalid, page posts to the server; however, it's easy to get around client side validation so make sure you do server validation as well.

When you receive the data on the server......test it to make sure that the data meets the validation conditions (your requirements) before trying to use it (especially before accessing a database).

On the server, if any validation fails, print error messages prompting the user to make the necessary corrections onto the page and send the page back to the user.

In the browser, call a JavaScript method that validates the data being submitted before it's submitted...if the data is not valid then cancel the page submit and display messages prompting the user to correct any invalid data...otherwise let it submit to the server.
Jul 7 '09 #2
Thanks for the above reply... But in this case i need to validate a Employee ID number entered into a text box against a database table to check whether the ID number exists in the database, if the ID number doesn't exist in database i should not allow him to submit the form, need to print error message..I am using Active Server pages for form design, JavaScript for server side validation and VBScript for Database connectivity.

How can i validate a text box value against database table using ASP and VB Script..Please advise

Thanks again,
Chandru
Jul 7 '09 #3
Markus
6,050 Expert 4TB
What kind of database is it?
Jul 8 '09 #4
It is an sql server database.
Jul 8 '09 #5
Frinavale
9,735 Expert Mod 8TB
Well you could use SQL to query the number records with the Employee number provided and check if there are any matching records.....

You could use the SQL Count() function to do this.

For example:

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(EmployeeIDs) AS NumEmpIDs FROM Employees
  2.   WHERE ID=theValueInTheTextBox
If the value returned from that select statement is 0 then there is no matching employee ID's and you need to display the message.
Aug 5 '09 #6
Thanks for your response..

As SQL Count() function is used to get the number of empID's in the table, how will it check whether the employee ID entered by user(Front end) exists in the database??

Might be a silly question, but want to have a better understanding? Can you please advise?

Thanks again :)
Aug 6 '09 #7
Frinavale
9,735 Expert Mod 8TB
I didn't realise what you were asking.

Umm...yeah...I don't know how to do this with classic ASP.

I'm moving the question to the ASP forum so you can get more help :) :)
Aug 6 '09 #8
Frinavale
9,735 Expert Mod 8TB
Well actually one thing that I can think of is to make an ASP page simply used to check the employee id.

In that ASP page you'd have a method that validates the ID provided against the database. If it finds something wrong then it returns an error message, otherwise it returns an empty string.

You'd call this method using Ajax, passing it the value that was entered in the text box.

You still have to send this information to the server so that it can validate against the database, but you don't have to post the whole page back to the server to do so. All you'd be doing is making a very small request, moving a small bit of data from client to the server and back.

The following is an example of what would be in the validate.asp page. Please note that this is my second time every using ASP classic so all that it does is take the employee ID provided and sends it back to the user. You'll have to modify this so that it checks if the ID provided exists in the database (using the SQL Count() method we talked about earlier) and base the return off of that result. I don't know how to connect to and us a database in asp classic at this time (I feel like a fish out of water here) but I'm sure it's pretty simple. Here's the validate.asp:
Expand|Select|Wrap|Line Numbers
  1. <%  Response.Buffer = true 
  2.     Response.Expires=-1
  3.     Response.CacheControl="no-cache"
  4.     dim _id
  5.     _id=Request.QueryString("empID")
  6.  
  7.     Function Validate() 
  8.         'Here you need to do your validation against the database.
  9.         Response.Write("The EmployeeID Provided Was:" + _id)
  10.     End Function 
  11.  
  12.  
  13.        Call Validate() 
  14. %>
The following JavaScript code has to go in your normal ASP page. You need to call the validateEmpID() JavaScript method when the user's done entering the employee id....maybe on the JavaScript onblur event for the textbox. Please note that right now the validateEmpID() method assumes that you have a textbox on the page with the id "TextBoxWithEmpID". The validateEmpID() method makes an Ajax call to the validate.asp page passing it the value provided in the TextBox. When the Ajax call returns, this JavaScript displays an alert with what the validate.asp page returned:
Expand|Select|Wrap|Line Numbers
  1. var xmlHttp;
  2.  
  3. function validateEmpID()
  4. {    
  5.   /*  Instanciating the HttpRequest object that will be used to make the Ajax calls.  See http://www.w3.org/TR/XMLHttpRequest/ for more information on this object.*/
  6.  
  7.   xmlHttp=GetXmlHttp();
  8.  
  9.   /*  validate.asp is an ASP page has been created for the sole purpose of validating the employee ID.*/
  10.   var urlRequest = "http://localhost/validate.asp?empID=" + document.getElementByID('TextBoxWithEmpID').value;
  11.   xmlHttp.open('GET', urlRequest , true); 
  12.  
  13.   //making the request
  14.    xmlHttp.send(null); 
  15. };
  16.  
  17. xmlHttp.onreadystatechange=function()
  18. {    /* Make sure that the transaction has finished. 
  19.             The XMLHttpRequest object 
  20.           has a property called readyState with several states:
  21.                 0: Uninitialized
  22.                 1: Loading
  23.                 2: Loaded
  24.                 3: Interactive
  25.                 4: Finished */
  26.     if(xmlHttp.readyState == 1)
  27.     {    
  28.     }
  29.     if(xmlHttp.readyState == 4)
  30.     { //Finished loading the response
  31.  
  32.         /* We have got the response from the server-side script,
  33.             let's see just what it was. using the responseText property of 
  34.             the XMLHttpRequest object. */        
  35.         try
  36.         {
  37.             if(xmlHttp.status == 200){    
  38.                 alert(xmlHttp.responseText;);
  39.             }else{
  40.                 alert('There was a problem validating');
  41.             }
  42.         }catch(e){    
  43.             alert("An error occrred:"+e.description);
  44.         }
  45. };
  46.  
  47.  
  48.  
  49. function GetXmlHttp()
  50. {    /*This function is responsible for creating an HttpRequest object 
  51.     based on the browser that the user is currently using. */
  52.     var xmlHttp = null;            
  53.     try
  54.     {   //Mozilla, Opera, Safari etc.
  55.         xmlHttp=XMLHttpRequest();
  56.      }catch(e)
  57.     {   //Internet Explorer uses ActiveX objects to make Ajax calls.
  58.         //the following are valid versions, here we will loop through 
  59.         //the versions and attempt to create the ActiveX that matches the browser.
  60.  
  61.         var versionIds = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
  62.                     "Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0", 
  63.                     "Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0", 
  64.                          "Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
  65.         for(var i=0; i<versionIds.length && xmlHttp == null; i++) 
  66.         {
  67.             xmlHttp = CreateXmlHttp(versionIds[i]);
  68.         }
  69.     }
  70.     return xmlHttp;
  71. }
  72.  
  73. function CreateXmlHttp(id) 
  74. {   /*Creates an ActiveX object used by Internet Explorer that will make Ajax calls*/
  75.     var xmlHttp = null;
  76.     try 
  77.     {
  78.         xmlHttp = new ActiveXObject(id);
  79.     }catch(e) {}
  80.     return xmlHttp;
  81. }
I hope this answers your question because it was a lot of fun finding the answer....I don't think I'm prepared to continue into asp classic at this time...so maybe the experts in this forum (the asp classic forum) can help you further.
Aug 6 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: sjl | last post by:
I've got an .aspx webform for searching my database. It basically takes user input and passes it as a parm into a stored proc to search a table. The results are returned in a SQLDataReader and...
4
by: Mike Fellows | last post by:
running IIS on a single server, hosting pages on an intranet basis, one single user out of 50 is having an unable to validate data issue how do i fix this (the microsoft KB is a little...
15
by: philip | last post by:
On a form, I have a datagridview. This datagridview is constructed on a dataset filled by a tableadapter. The table adapter do very well what it must do when filling dataset. Insertions,...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
2
by: Rich Kayton | last post by:
In an order entry system I want to validate that the company ID entered is one that exists in a related table. I don't want to use a lookup value since the list would be too long. I just want to...
1
by: yasinirshad | last post by:
HI, Can anyone tel me How to copy data from a table in one database to a table in another database using sqlserver2005. Thanks in advance.
1
by: Milan Mehta | last post by:
I am a newbie to MS Access. Please bear with me for such question. I have a table1 in Database1. I need to append data from table2 in Database2 to table1 of Databases1. How can I do that ? TIA...
10
by: chandhseke | last post by:
I have developed a web page using ASP classic, I am using VBScript for database connectivity... The web application is working fine, but i am now working on a new enhancement requirement where i need...
3
by: abhishek1234321 | last post by:
I have a form at http://www.vtuhub.com/w.html which shows results if a proper roll number is entered... the results are fetched from http://results.vtu.ac.in site and only the table is shown... now i...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.