Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

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
0
Uugeman
Asked:
Uugeman
1 Solution
 
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
Industry Leaders: 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!

 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now