Solved

Validating Form data agains a database record.

Posted on 2003-11-05
11
371 Views
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.

Thanks
0
Comment
Question by:Uugeman
11 Comments
 
LVL 10

Expert Comment

by:KenAdney
ID: 9689143
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
 

Author Comment

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

Expert Comment

by:KenAdney
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?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

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

Accepted Solution

by:
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">
</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
 
LVL 3

Expert Comment

by:red010knight
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;
  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
 

Author Comment

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

Author Comment

by:Uugeman
ID: 9690529
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
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9690727
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
 
LVL 10

Expert Comment

by:KenAdney
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".
0
 

Author Comment

by:Uugeman
ID: 9695966
Thats what I did! :)

Thanks everyone!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now