Validating Form data agains a database record.

Posted on 2003-11-05
Last Modified: 2010-04-06
I need to take the value of a text field and check to see if it is in a Database. I then need to validate that the same field value is 8 characters.

Can someone show me sample code to do this?

I am new to this type of validation.

Question by:Uugeman
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
LVL 10

Expert Comment

ID: 9689143
How about...

If request.form("textfield") <> rs("database field")
Then response.write("the fields do not match")


If Len(textfield)<>8 Then
      error_message = ("<p>It has got to be 8 characters</p>")
End If



Author Comment

ID: 9689190
How can I create a function to do this onsubmit?
LVL 10

Expert Comment

ID: 9689253
Sorry.  I leaped to the assumption of doing with VBscript in ASP.  Obviously this has to be server side coding (since you're checking against a database).  The way I'd do it is after the user hits the Submit Form button, I'd check to be sure the field isn't blank, then open the database & see if the data is there (my first example), then I'd check the length of the text field.  Do you want it to update the database if isn't there & is 8 characters long?  Will ASP work for you?
Technology Partners: 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!


Author Comment

ID: 9689275
I dont want to update. I just want to verify that the user is inputting the correct number (one that exists in the database) If its not the correct number I want to give them a message box. If it is then I will show them a set of results based on their input.
LVL 10

Accepted Solution

KenAdney earned 50 total points
ID: 9689368
Here's a more complete version of the ASP code.  I didn't test it & I think IF statements may need to be ended more correctly, but:

<form action="default.asp" method="post">
<input type="text" name="formfield" size="8" maxlength="9">
<input type="submit" name="submit">
<% If request.form("formfield) <> "" THEN
Set DataConn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")

DataConn.Open "DBQ=" & Server.Mappath("database.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"

sql = "SELECT textfield FROM tablename"
rs.Open sql, DataConn, 1, 3

if request.form("formfield") = then rs.Fields("textfield")    
then response.write("it is already in the database")

If Len(textfield)<>8 Then
     error_message = ("<p>It has got to be 8 characters</p>")
End If

response.write ("you left the field blank")

SET rs = Nothing
Set DataConn = Nothing

Expert Comment

ID: 9689966
Well here is a suggestion - how about checking that the data is of proper format prior to submitting the form?

For this you would need a javascript function

<script language="javascript">
function verifyForm(){
  var testString=document.myForm.myField.value.length;
    return true;
    return false;

<form name="myForm" action="fileThatCallsDatabase" method="post" onSubmit="javascript:verifyForm();">

if onsubmit gets false the submit process should be aborted if I did the code right. I don't have a setup to test it. But I did verify the commands so it should work.

As for the database verification, you need to use a server side script. But from what I understand, the value you are checking in the database needs to be 8 characters long, so why make a database call after the database check? Wouldn't that be a little redundant?

If you can tell me what database and languages you are able to use, I may be able to help you further.

Just my two cents, hope the script helps you!

Author Comment

ID: 9690433
Ken will this work onsubmit or should I just plug it in after the form?

Author Comment

ID: 9690529
I am now getting an error on line 22,

Error Type:
Microsoft VBScript compilation (0x800A03F6)
Expected 'End'

Whats wrong with this statement?

Option Explicit
Response.Buffer = True
'On Error Resume Next
Call OpenConnection()
Dim rstProjectNum, strSQLProjectNum, strUserProjNumber
      strUserProjNumber = Request.Form("txtProjectNumber")
      strSQLProjectNum = "SELECT * FROM tblProject WHERE strProjectNumber='" & strUserProjNumber & "'"
      Call LoadRecordSet(rstProjectNum, strSQLProjectNum)
            If request.form("txtProjectNumber") = rstProjectNum.Fields("strProjectNumber") Then
                        response.write("SOME DATA HERE")

                        If Len(textfield)<>4 Then
                       Msgbox("The project number has to be 4 characters")
                              Response.Redirect ("PM_inputproject.asp")
                        End If

                        'Msgbox ("you left the field blank")
                        'Response.Redirect ("PM_inputproject.asp")
            End If
Call CloseConnection()
LVL 15

Expert Comment

ID: 9690727

RedKnight is correct in that the length comparison should take place on the client.  Why do you want to waste the user's time with a server call?

<script language="javascript" type='text/javascript'>

function verifyForm(fieldObj)
  if (8 != fieldObj.value.length)
      alert( + ' must be 8 characters')
      return false;
 else return true;

<form name="myForm" action="fileThatCallsDatabase" method="post" onSubmit="return(verifyForm(this.fieldname)">


LVL 10

Expert Comment

ID: 9695942
Thanks for the points.  I agree with the folks above that I'd check for 8 characters on the client before I ran the server side check against the database.  But to answer your question, in the example I gave, the form is straight HTML so you can use the form you've got.  I just included it so you could see that I called the data element in the form "formfield".

Author Comment

ID: 9695966
Thats what I did! :)

Thanks everyone!

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

718 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