Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation

Status
Not open for further replies.

Floodster

Technical User
Joined
Jan 28, 2005
Messages
204
Can anyone find out why this won't work, if i limit the validation to 1 field everything works fine, if i try & validate more then 1 then i get OBJECT ERROR. Here is the validation code;
-------------------------
<script language="JavaScript" >
<!--
function validate_search(field,alerttxt)
{
with (field)
{

if (value=="")
{alert(alerttxt);return false}
else {return true}
}
}

function validate_form2(fadd)
{
with (fadd)
{
if (validate_search(ftitle,"Title cannot be blank")==false)
{ftitle.focus();return false}

if (validate_search(fdescription,"Description cannot be blank")==false)
{fdescription.focus();return false}

if (validate_search(fmetadata,"Metadata cannot be blank")==false)
{fmetadata.focus();return false}

if (validate_search(fmost,"Most Requested cannot be blank")==false)
{fmost.focus();return false}

if (validate_search(flink,"Link cannot be blank")==false)
{flink.focus();return false}
}
}
//-->
</script>
------------------------------------
Here is the code for my form
------------------------------------
<form ACTION="<%=MM_editAction%>" METHOD="POST" name="fadd" id="fadd" onsubmit="return validate_form2(fadd)">
<table border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><p>Title</p></td>
<td><p>
<input name="ftitle" type="text" id="ftitle" size="50" maxlength="50">
</p></td>
</tr>
<tr>
<td><p>Description</p></td>
<td><p>
<input name="fdescription" type="text" id="fdescription" size="50" maxlength="255">
</p></td>
</tr>
<tr>
<td><p>Metadata</p></td>
<td><p>
<input name="fmetadata" type="text" id="fmetadata" size="50" maxlength="255">
</p></td>
</tr>
<tr>
<td><p>Most Requested</p></td>
<td> <p>
<input name="fmost" type="text" id="fmost" size="3" maxlength="1">
Y or N</p></td>
</tr>
<tr>
<td><p>Area</p></td>
<td><p>
<input name="farea" type="text" id="farea" size="20" maxlength="20">
</p></td>
</tr>
<tr>
<td><p>Updated by</p></td>
<td><p>
<input name="fupdatedby" type="text" id="fupdatedby" value="<%= Session("MM_Username") %>" size="15" maxlength="15">
e.g jflood</p></td>
</tr>
<tr>
<td><p>Date</p></td>
<td><p>
<input name="fdate" type="text" id="fdate" value="<%=date()%>" size="18" maxlength="10">
e.g 31-jan-06</p></td>
</tr>
<tr>
<td><p>Link</p></td>
<td><p>
<input name="flink" type="text" id="flink" size="50" maxlength="50" />
e.g research.htm</p></td>
</tr>
</table>
<p align="center"> <%=now()%>
<input name="SubAdd" type="submit" id="SubAdd" value="Add" />
</p>
<input type="hidden" name="MM_insert" value="fadd">
</form>
-----------------------------------
i'm tearing my hair out on this one, please help.
thanks,
 
>[tt]onsubmit="return validate_form2(fadd)"[/tt]
[tt]onsubmit="return validate_form2([red]this[/red])"[/tt]
 
tsuji,
this makes no difference I'm still getting the same error!?!?!?!?!?
thanks,
 
Floodster,

When you say that you want to "validate more then 1", you'll need to be clearer.

Do you mean that you :

1. want to validate multiple fields, and give multiple errors at the same time,

2. want to validate multiple fields, giving the errors 1 at a time (as you do at the moment), or

3. something else?

If 2, then your code works just fine. If it's not working for you, then it may be down to a spurious value injected by the server-side code.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan,
Yes it is option 2 I want but I can't get it to work, even if I create a blank document & start from fresh???
 
The code you posted works perfectly for me if I do the following:

- Add an opening html and head tag
- Add a closing head tag
- Add an open bodyy tag
- Add closing body and html tags
- Replace the 3 bits of server-side includes with hard-coded data.

Try those - it should work for you.

As I said, it's probably something you're outputting server-side. Why not show us the client-side source? At least that way, we'd have an idea of what the browser is seeing. At present, we can only guess.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan,
Here is the code in it's full.
-------------------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/ResearchDB_custom.asp" -->
<%
' *** Edit Operations: declare variables

Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd

Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId

Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i

MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Request.QueryString
End If

' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: set variables

If (CStr(Request("MM_insert")) = "fadd") Then

MM_editConnection = MM_ResearchDB_custom_STRING
MM_editTable = "datasets"
MM_editRedirectUrl = "login_accepted.asp"
MM_fieldsStr = "ftitle|value|fdescription|value|fmetadata|value|fmost|value|farea|value|fupdatedby|value|fdate|value|flink|value"
MM_columnsStr = "Title|',none,''|Description|',none,''|Metadata|',none,''|most_requested|',none,''|Area|',none,''|Updated_by|',none,''|dDate|',none,NULL|link|',none,''"

' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "|")
MM_columns = Split(MM_columnsStr, "|")

' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
Next

' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And Request.QueryString <> "") Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If

End If
%>
<%
' *** Insert Record: construct a sql insert statement and execute it

Dim MM_tableValues
Dim MM_dbValues

If (CStr(Request("MM_insert")) <> "") Then

' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),",")
MM_delim = MM_typeArray(0)
If (MM_delim = "none") Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none") Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none") Then MM_emptyVal = ""
If (MM_formVal = "") Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> "") Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'") Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","''") & "'"
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"

If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editRedirectUrl)
End If
End If

End If
%>
<%
Dim update_details__MMColParam
update_details__MMColParam = "1"
If (Request.QueryString("ID") <> "") Then
update_details__MMColParam = Request.QueryString("ID")
End If
%>
<%
Dim update_details
Dim update_details_numRows

Set update_details = Server.CreateObject("ADODB.Recordset")
update_details.ActiveConnection = MM_ResearchDB_custom_STRING
update_details.Source = "SELECT * FROM datasets WHERE ID = " + Replace(update_details__MMColParam, "'", "''") + ""
update_details.CursorType = 0
update_details.CursorLocation = 2
update_details.LockType = 1
update_details.Open()

update_details_numRows = 0
%>
<html xmlns="<head>
<title>Add new record details</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../css/menu.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" >
<!--
function validate_search(field,alerttxt)
{
with (field)
{

if (value=="")
{alert(alerttxt);return false}
else {return true}
}
}

function validate_form2(fadd)
{
with (fadd)
{
if (validate_search(ftitle,"Title cannot be blank")==false)
{ftitle.focus();return false}

if (validate_search(fdescription,"Description cannot be blank")==false)
{fdescription.focus();return false}

if (validate_search(fmetadata,"Metadata cannot be blank")==false)
{fmetadata.focus();return false}

if (validate_search(fmost,"Most Requested cannot be blank")==false)
{fmost.focus();return false}

if (validate_search(flink,"Link cannot be blank")==false)
{flink.focus();return false}
}
}
//-->
</script>
</head>

<body>


<span id="Layer1" style="position:absolute; width:700px; height:182px; z-index:1; left: 185; top: 89;">
<h2 align="left">Add New Record</h2>

<form ACTION="<%=MM_editAction%>" METHOD="POST" name="fadd" id="fadd" onsubmit="return validate_form2(fadd)">
<table border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><p>Title</p></td>
<td><p>
<input name="ftitle" type="text" id="ftitle" size="50" maxlength="50">
</p></td>
</tr>
<tr>
<td><p>Description</p></td>
<td><p>
<input name="fdescription" type="text" id="fdescription" size="50" maxlength="255">
</p></td>
</tr>
<tr>
<td><p>Metadata</p></td>
<td><p>
<input name="fmetadata" type="text" id="fmetadata" size="50" maxlength="255">
</p></td>
</tr>
<tr>
<td><p>Most Requested</p></td>
<td> <p>
<input name="fmost" type="text" id="fmost" size="3" maxlength="1">
Y or N</p></td>
</tr>
<tr>
<td><p>Area</p></td>
<td><p>
<input name="farea" type="text" id="farea" size="20" maxlength="20">
</p></td>
</tr>
<tr>
<td><p>Updated by</p></td>
<td><p>
<input name="fupdatedby" type="text" id="fupdatedby" value="<%= Session("MM_Username") %>" size="15" maxlength="15">
e.g jflood</p></td>
</tr>
<tr>
<td><p>Date</p></td>
<td><p>
<input name="fdate" type="text" id="fdate" value="<%=date()%>" size="18" maxlength="10">
e.g 31-jan-06</p></td>
</tr>
<tr>
<td><p>Link</p></td>
<td><p>
<input name="flink" type="text" id="flink" size="50" maxlength="50" />
e.g research.htm</p></td>
</tr>
</table>
<p align="center"> <%=now()%>
<input name="SubAdd" type="submit" id="SubAdd" value="Add" />
</p>
<input type="hidden" name="MM_insert" value="fadd">
</form>
<p><a href="login_accepted.asp" target="_self">Back to Maintenance Options</a></p>
</span>
<!--#include file="header.asp" -->
</body>
</html>
<%
update_details.Close()
Set update_details = Nothing
%>
-------------------------------------------
can you see anything wrong???
 
Which bit of "client-side source" do you not get? That is server-side source, and is of no value whatsoever to someone trying to debug client-side problems.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Also, when posting code, you should put it inside TGML [ignore]
Code:
[/ignore] tags. For more on these, click the "Process TGML" link under the posting box.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan,
I have sorted it, it seems that the word "metadata" cannot be used in the code, instead "meta data" is accepted"

Can I just point out that as I have posted for help on this subject should indicate that I'm not an expert on it, so posting things like "which side of blah blah do you not get" is not very helpful or friendly.

I would imagine when I'm as good as you I will not make these novice mistakes.

Thanks for your suggestions anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top