C
Calan
I have a server-side ASP script that dynamically creates an input form from
a database table. The table contains a field name, the table where values
are stored, type of input control, value for a label, etc.
What I need to do is create a JS validation routine that will check each
control for valid input, regardless of what the control name is. If it is a
"select", it needs to verify the index is > 1. If it is an "input", it needs
to check that it isnt't empty, etc.
Using known fields, I usually do something like this (page name is "me.asp":
***** code *****
Server-side ASP:
<%
if not isempty (Request.Form("SEND")) then
if Request("validated") = "true" then
{do something}
response.redirect "nextpage.asp"
end if
end if
%>
Document head section:
<script language="JavaScript">
function validateComplete(formObj)
{
if (emptyField(formObj.OrderNumber))
alert("Please enter the order number.");
else if (formObj.BaseModel.selectedIndex < 1)
alert("Please select a Base Model.");
else if (formObj.Length.selectedIndex < 1)
alert("Please select a length.");
else if ... etc etc
else {
formObj.validated.value="true";
return true;
}
formObj.validated.value="false";
return false;
}
function emptyField(textObj)
{
if (textObj.value.length == 0) return true;
for (var i=0; i<textObj.value.length; ++i) {
var ch = textObj.value.charAt(i);
if (ch != ' ' && ch != '\t') return false;
}
return true;
}
</script>
And then in the form definition:
<form method="POST" action="me.asp" onSubmit="return validateComplete(this)"
language="JavaScript">
...
...
</form>
***** end code *****
How could I re-write this to work for any and all input controls on the
form, when the control names are not consistent?
Something like:
for each select, make sure the index is >= 1
else show alert with control name
return false
for each input, make sure it's not empty
else show alert with control name
return false
etc etc.
Thanks in advance!
Calan
AxMaster Guitar Software
www.jcsautomation.com
www.jcsautomation.com/music.asp
Music software and web design/hosting
"Reality exists only in the minds of the extremely deranged"
a database table. The table contains a field name, the table where values
are stored, type of input control, value for a label, etc.
What I need to do is create a JS validation routine that will check each
control for valid input, regardless of what the control name is. If it is a
"select", it needs to verify the index is > 1. If it is an "input", it needs
to check that it isnt't empty, etc.
Using known fields, I usually do something like this (page name is "me.asp":
***** code *****
Server-side ASP:
<%
if not isempty (Request.Form("SEND")) then
if Request("validated") = "true" then
{do something}
response.redirect "nextpage.asp"
end if
end if
%>
Document head section:
<script language="JavaScript">
function validateComplete(formObj)
{
if (emptyField(formObj.OrderNumber))
alert("Please enter the order number.");
else if (formObj.BaseModel.selectedIndex < 1)
alert("Please select a Base Model.");
else if (formObj.Length.selectedIndex < 1)
alert("Please select a length.");
else if ... etc etc
else {
formObj.validated.value="true";
return true;
}
formObj.validated.value="false";
return false;
}
function emptyField(textObj)
{
if (textObj.value.length == 0) return true;
for (var i=0; i<textObj.value.length; ++i) {
var ch = textObj.value.charAt(i);
if (ch != ' ' && ch != '\t') return false;
}
return true;
}
</script>
And then in the form definition:
<form method="POST" action="me.asp" onSubmit="return validateComplete(this)"
language="JavaScript">
...
...
</form>
***** end code *****
How could I re-write this to work for any and all input controls on the
form, when the control names are not consistent?
Something like:
for each select, make sure the index is >= 1
else show alert with control name
return false
for each input, make sure it's not empty
else show alert with control name
return false
etc etc.
Thanks in advance!
Calan
AxMaster Guitar Software
www.jcsautomation.com
www.jcsautomation.com/music.asp
Music software and web design/hosting
"Reality exists only in the minds of the extremely deranged"