By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

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

P: 92
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?

Jun 30 '09 #1
Share this Question
Share on Google+
8 Replies

Expert Mod 5K+
P: 9,731
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

P: 92
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,
Jul 7 '09 #3

Expert 5K+
P: 6,050
What kind of database is it?
Jul 8 '09 #4

P: 92
It is an sql server database.
Jul 8 '09 #5

Expert Mod 5K+
P: 9,731
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

P: 92
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

Expert Mod 5K+
P: 9,731
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

Expert Mod 5K+
P: 9,731
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")
  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 
  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;
  3. function validateEmpID()
  4. {    
  5.   /*  Instanciating the HttpRequest object that will be used to make the Ajax calls.  See for more information on this object.*/
  7.   xmlHttp=GetXmlHttp();
  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.'GET', urlRequest , true); 
  13.   //making the request
  14.    xmlHttp.send(null); 
  15. };
  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
  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. };
  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.
  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. }
  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 maybe the experts in this forum (the asp classic forum) can help you further.
Aug 6 '09 #9

Post your reply

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