Validating Form data agains a database record.

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.

Thanks
UugemanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KenAdneyCommented:
How about...

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

and

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

response.write(error_message)

0
UugemanAuthor Commented:
How can I create a function to do this onsubmit?
0
KenAdneyCommented:
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?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

UugemanAuthor Commented:
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.
0
KenAdneyCommented:
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">
</form>
<% 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")
ELSE

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

ELSE
response.write ("you left the field blank")
END IF

SET rs = Nothing
DataConn.Close
Set DataConn = Nothing
%>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
red010knightCommented:
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;
  if(testString==8){
    return true;
  }else{
    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!
Red010Knight
0
UugemanAuthor Commented:
Ken will this work onsubmit or should I just plug it in after the form?
0
UugemanAuthor Commented:
I am now getting an error on line 22,

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

Whats wrong with this statement?

<% @LANGUAGE="VBSCRIPT" %>
<%
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")
                  Else

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

                  Else
                        'Msgbox ("you left the field blank")
                        'Response.Redirect ("PM_inputproject.asp")
            End If
Call CloseConnection()
%>
0
VincentPugliaCommented:
Hi,

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(fieldObj.name + ' must be 8 characters')
      fieldObj.focus();
      return false;
  }
 else return true;
}


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



Vinny

0
KenAdneyCommented:
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".
0
UugemanAuthor Commented:
Thats what I did! :)

Thanks everyone!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.