Uugeman
asked on
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
Can someone show me sample code to do this?
I am new to this type of validation.
Thanks
ASKER
How can I create a function to do this onsubmit?
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?
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.v alue.lengt h;
if(testString==8){
return true;
}else{
return false;
}
}
<form name="myForm" action="fileThatCallsDatab ase" method="post" onSubmit="javascript:verif yForm();">
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
For this you would need a javascript function
<script language="javascript">
function verifyForm(){
var testString=document.myForm
if(testString==8){
return true;
}else{
return false;
}
}
<form name="myForm" action="fileThatCallsDatab
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
ASKER
Ken will this work onsubmit or should I just plug it in after the form?
ASKER
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("txtProjectNu mber")
strSQLProjectNum = "SELECT * FROM tblProject WHERE strProjectNumber='" & strUserProjNumber & "'"
Call LoadRecordSet(rstProjectNu m, strSQLProjectNum)
If request.form("txtProjectNu mber") = rstProjectNum.Fields("strP rojectNumb er") 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()
%>
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("txtProjectNu
strSQLProjectNum = "SELECT * FROM tblProject WHERE strProjectNumber='" & strUserProjNumber & "'"
Call LoadRecordSet(rstProjectNu
If request.form("txtProjectNu
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()
%>
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="fileThatCallsDatab ase" method="post" onSubmit="return(verifyFor m(this.fie ldname)">
Vinny
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="fileThatCallsDatab
Vinny
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".
ASKER
Thats what I did! :)
Thanks everyone!
Thanks everyone!
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_messa