473,408 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Input validation for "unknown" field names

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"
Jul 23 '05 #1
8 2000
Also, I am strong in ASP, but not JS... :-)

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

"Calan" <ca*************@yaNOSPAMhoo.com> wrote in message
news:1x***************@fe25.usenetserver.com...
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"

Jul 23 '05 #2
On Sun, 4 Apr 2004 12:24:08 -0500, Calan <ca*************@yaNOSPAMhoo.com>
wrote:

[snip]
<script language="JavaScript">
The language attribute is deprecated. Use the required type attribute:

<script type="text/javascript">

[snip]
<form method="POST" action="me.asp" onSubmit="return
validateComplete(this)" language="JavaScript">
FORM elements do not, and never have, had a language attribute.

[snip]
How could I re-write this to work for any and all input controls on the
form, when the control names are not consistent?


Use the obj.type attribute to determine the control type, and act
accordingly. For example,

function checkForm( form )
{
var c = form.elements, e, t;

for( var i = 0, n = c.length; i < n; ++i )
{
t = ( e = c[ i ]).type;

if(( 'select-one' == t ) && ( 0 == e.selectedIndex ))
{
// Tell user to select option
}
else if(( 'text' == t ) && ( 0 > e.value.length ))
{
// Tell user to enter text
}
// etc.
}
}

A list of the types for each form control is given with each control in
W3C's DOM HTML Specification, Netscape's JavaScript Reference (v1.3 only),
and Microsoft's DHTML Reference (watch for wrap):

DOM:
<URL:http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-html.html#ID-798055546>

JS Ref:
<URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/>

DHTML:
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects.asp>

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #3
Mike....

Being a JS newbie, could you give a short blurb on what each line is doing?
I'm a bit confused as to what 'select-one' and 'text' is referring to, and
what the 't = ( e = c[ i ]).type' line is doing.

Thanks much!

Calan

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Sun, 4 Apr 2004 12:24:08 -0500, Calan <ca*************@yaNOSPAMhoo.com>
wrote:

[snip]
<script language="JavaScript">
The language attribute is deprecated. Use the required type attribute:

<script type="text/javascript">

[snip]
<form method="POST" action="me.asp" onSubmit="return
validateComplete(this)" language="JavaScript">


FORM elements do not, and never have, had a language attribute.

[snip]
How could I re-write this to work for any and all input controls on the
form, when the control names are not consistent?


Use the obj.type attribute to determine the control type, and act
accordingly. For example,

function checkForm( form )
{
var c = form.elements, e, t;

for( var i = 0, n = c.length; i < n; ++i )
{
t = ( e = c[ i ]).type;

if(( 'select-one' == t ) && ( 0 == e.selectedIndex ))
{
// Tell user to select option
}
else if(( 'text' == t ) && ( 0 > e.value.length ))
{
// Tell user to enter text
}
// etc.
}
}

A list of the types for each form control is given with each control in
W3C's DOM HTML Specification, Netscape's JavaScript Reference (v1.3 only),
and Microsoft's DHTML Reference (watch for wrap):

DOM:

<URL:http://www.w3.org/TR/1998/REC-DOM-Le...one-html.html#
ID-798055546>
JS Ref:
<URL:http://devedge.netscape.com/library/...pt/1.3/referen
ce/>
DHTML:
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects.asp>
Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

Jul 23 '05 #4
On Mon, 5 Apr 2004 14:58:29 -0500, Calan <no**@nospam.com> wrote:
Being a JS newbie, could you give a short blurb on what each line is
doing?
Certainly!
I'm a bit confused as to what 'select-one' and 'text' is referring to,
and what the 't = ( e = c[ i ]).type' line is doing.
That assignment can also be written

e = c[ i ];
t = e.type;

c - A reference to the collection of form controls in the form passed to
the function.
e - The current form control that's being validated.
t - The type of that form control.

The strings you mention appear in the type attribute for specific form
controls. 'text' signifies that the control is an INPUT element of type
text. 'select-one' represents a SELECT menu. There is a similar string,
'select-multiple', which is a SELECT element that specifies the multiple
attribute. As I said in my previous post, all form controls have a unique
string that allows them to be identified via the type attribute.
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
function checkForm( form )
{
var c = form.elements, e, t;

for( var i = 0, n = c.length; i < n; ++i )
{
t = ( e = c[ i ]).type;
Once you've established the type, you can then apply whatever validation
checks you want...
if(( 'select-one' == t ) && ( 0 == e.selectedIndex ))
{
// Tell user to select option
}
else if(( 'text' == t ) && ( 0 > e.value.length ))
{
// Tell user to enter text
}
// etc.
}
}


Is that any better?

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #5
Excellent Mike... one more question... (of course) :-)

Can I use the value of the control's associated label in the warning message
somehow? For example, I use an ASP variable called "LabelText" (which is
part of the field definition in a database) to write a label next to the
control. Something like:

<%
blah blah
response.write "<td><span class='" & LabelClass & "'>" & LabelText &
"</span>
response.write "<" & ControlType & "name=' & ControlName & "' value='" &
CurrentValue & "'></td>"
etc etc etc
%>

I know I could probably display the control's name (e.name or something
similar?), but since these are database field equivalents, they are somewhat
archaic

Is there some way to capture the "LabelText" value and display it
client-side, so that the alert describes which control is being checked? (My
first thought is to use a hidden form control, named "Label" with
value=LabelText. But I'm not quite sure how to tie the label to each
control/ I just got home and haven't had enough beers to think clearly
yet)....

Now that I think about it as I type, there is another issue in that not all
controls are required. (There is an associated ASP variable called
"Required" for each control, which is used to change the label CSS class
from balck to red; shown as "LabelClass" in the example above). I'll need to
check for this also somehow. It just gets better and better... :-)

Thanks again!

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

Jul 23 '05 #6
Excellent Mike... one more question... (of course) :-)

Can I use the value of the control's associated label in the warning message
somehow? For example, I use an ASP variable called "LabelText" (which is
part of the field definition in a database) to write a label next to the
control. Something like:

<%
blah blah
response.write "<td><span class='" & LabelClass & "'>" & LabelText &
"</span>
response.write "<" & ControlType & "name=' & ControlName & "' value='" &
CurrentValue & "'></td>"
etc etc etc
%>

I know I could probably display the control's name (e.name or something
similar?), but since these are database field equivalents, they are somewhat
archaic

Is there some way to capture the "LabelText" value and display it
client-side, so that the alert describes which control is being checked? (My
first thought is to use a hidden form control, named "Label" with
value=LabelText. But I'm not quite sure how to tie the label to each
control/ I just got home and haven't had enough beers to think clearly
yet)....

Now that I think about it as I type, there is another issue in that not all
controls are required. (There is an associated ASP variable called
"Required" for each control, which is used to change the label CSS class
from balck to red; shown as "LabelClass" in the example above). I'll need to
check for this also somehow. It just gets better and better... :-)

Thanks again!

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


Jul 23 '05 #7
On Mon, 5 Apr 2004 19:01:39 -0500, Calan <ca*************@yaNOSPAMhoo.com>
wrote:
Can I use the value of the control's associated label in the warning
message somehow? For example, I use an ASP variable called "LabelText"
(which is part of the field definition in a database) to write a label
next to the control. Something like:

<%
blah blah
response.write "<td><span class='" & LabelClass & "'>" & LabelText &
"</span>
response.write "<" & ControlType & "name=' & ControlName & "' value='" &
CurrentValue & "'></td>"
etc etc etc
%>

I know I could probably display the control's name (e.name or something
similar?), but since these are database field equivalents, they are
somewhat archaic

Is there some way to capture the "LabelText" value and display it
client-side, so that the alert describes which control is being checked?
(My first thought is to use a hidden form control, named "Label" with
value=LabelText. But I'm not quite sure how to tie the label to each
control/ I just got home and haven't had enough beers to think clearly
yet)....
I thought you might ask about this. There are a couple of approaches. One
"abuses" the title attribute, the other takes advantage of semantic
mark-up, specifically, the LABEL element. Though the latter might be more
desirable, it requires more recent browsers to work. I'll present both and
let you use decide which one you want.

Using the title attribute.

This is really simple: concatenate obj.title into the error message. For
example (using the script from my previous post),

/* This was made a function to be consistent
throughout the rest of this post
*/
function getLabel( e )
{
return e.title;
}

if(( 'select-one' == t ) && ( 0 == e.selectedIndex ))
{
alert( "Please choose an option from the '" +
getLabel( e ) + "' menu" );
}

Base Model <select ... title="Base Model">
...
</select>
Using LABEL.

This builds nicely on to your current code generation (at least if it
follows the ASP snippet above). In fact, it would make a lot more sense if
you did use the LABEL element:

<%
response.write "<td><label for='" & ControlId & "'>" & LabelText &
"</label>"
response.write "<" & ControlType & "id='" & ControlId &
"' name='" & ControlName & "' value='" & CurrentValue & "'></td>"
%>

where ControlId could just be a nonsensical value like "id037", used
solely for the purpose of linking the label with the element[1]. You can
then style LABEL elements in general, rather than SPAN elements with a
certain class.

With this established, you can either walk the document tree, or search
through a collection of LABEL elements, to find the label text:

function getLabel( e )
{
var n = e.previousSibling;

while( n && ( 'LABEL' != n.nodeName ))
{
n = n.previousSibling;
}
if( n && n.firstChild )
{
return n.firstChild.nodeValue;
}
}

OR

function getLabel( e )
{
var c, id = e.id, l = null;

if( document.getElementsByTagName )
{
l = document.getElementsByTagName( 'label' );
}
else if( document.all && document.all.tags )
{
l = document.all.tags( 'label' );
}
if( l )
{
for( var i = 0, n = l.length; i < n; ++i )
{
c = l[ i ];
if( c.for == id )
{
if( c.firstChild )
{
return c.firstChild.nodeValue;
}
return;
}
}
}
}

if(( 'select-one' == t ) && ( 0 == e.selectedIndex ))
{
alert( "Please choose an option from the '" +
getLabel( e ) + "' menu" );
}

<label for="id13">Base Model</label>
<select id="id13" ... title="Base Model">
...
</select>
Now that I think about it as I type, there is another issue in that not
all controls are required. (There is an associated ASP variable called
"Required" for each control, which is used to change the label CSS class
from balck to red; shown as "LabelClass" in the example above). I'll
need to check for this also somehow. It just gets better and better...
:-)


This is another regular consideration. You could do this by giving
required controls a "required" class name. The class attribute can contain
more than one class name by separating them with whitespace. For example,

function isRequired( e )
{
return( -1 != e.className.indexOf( 'required' ));
}

if(( 'select-one' == t ) && ( 0 == e.selectedIndex ) &&
isRequired( e ))
{
alert( "The " + getLabel( e ) + " menu is a required field." +
"\nPlease choose an option before submitting." );
}

Hope that helps,
Mike
[1] You can bind a label to an element just by including the element
within the label:

<label>A control <input type="text"></label>

However, I've read that IE doesn't support this and the link must be made
with the for and id attributes.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #8
wow.... this is excellent info Mike

Thanks again for all the help...!

Craig

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Mon, 5 Apr 2004 19:01:39 -0500, Calan <ca*************@yaNOSPAMhoo.com>
wrote:
Can I use the value of the control's associated label in the warning
message somehow? For example, I use an ASP variable called "LabelText"
(which is part of the field definition in a database) to write a label
next to the control. Something like:

<%
blah blah
response.write "<td><span class='" & LabelClass & "'>" & LabelText &
"</span>
response.write "<" & ControlType & "name=' & ControlName & "' value='" &
CurrentValue & "'></td>"
etc etc etc
%>

I know I could probably display the control's name (e.name or something
similar?), but since these are database field equivalents, they are
somewhat archaic

Is there some way to capture the "LabelText" value and display it
client-side, so that the alert describes which control is being checked?
(My first thought is to use a hidden form control, named "Label" with
value=LabelText. But I'm not quite sure how to tie the label to each
control/ I just got home and haven't had enough beers to think clearly
yet)....


I thought you might ask about this. There are a couple of approaches. One
"abuses" the title attribute, the other takes advantage of semantic
mark-up, specifically, the LABEL element. Though the latter might be more
desirable, it requires more recent browsers to work. I'll present both and
let you use decide which one you want.

Using the title attribute.

This is really simple: concatenate obj.title into the error message. For
example (using the script from my previous post),

/* This was made a function to be consistent
throughout the rest of this post
*/
function getLabel( e )
{
return e.title;
}

if(( 'select-one' == t ) && ( 0 == e.selectedIndex ))
{
alert( "Please choose an option from the '" +
getLabel( e ) + "' menu" );
}

Base Model <select ... title="Base Model">
...
</select>
Using LABEL.

This builds nicely on to your current code generation (at least if it
follows the ASP snippet above). In fact, it would make a lot more sense if
you did use the LABEL element:

<%
response.write "<td><label for='" & ControlId & "'>" & LabelText &
"</label>"
response.write "<" & ControlType & "id='" & ControlId &
"' name='" & ControlName & "' value='" & CurrentValue & "'></td>"
%>

where ControlId could just be a nonsensical value like "id037", used
solely for the purpose of linking the label with the element[1]. You can
then style LABEL elements in general, rather than SPAN elements with a
certain class.

With this established, you can either walk the document tree, or search
through a collection of LABEL elements, to find the label text:

function getLabel( e )
{
var n = e.previousSibling;

while( n && ( 'LABEL' != n.nodeName ))
{
n = n.previousSibling;
}
if( n && n.firstChild )
{
return n.firstChild.nodeValue;
}
}

OR

function getLabel( e )
{
var c, id = e.id, l = null;

if( document.getElementsByTagName )
{
l = document.getElementsByTagName( 'label' );
}
else if( document.all && document.all.tags )
{
l = document.all.tags( 'label' );
}
if( l )
{
for( var i = 0, n = l.length; i < n; ++i )
{
c = l[ i ];
if( c.for == id )
{
if( c.firstChild )
{
return c.firstChild.nodeValue;
}
return;
}
}
}
}

if(( 'select-one' == t ) && ( 0 == e.selectedIndex ))
{
alert( "Please choose an option from the '" +
getLabel( e ) + "' menu" );
}

<label for="id13">Base Model</label>
<select id="id13" ... title="Base Model">
...
</select>
Now that I think about it as I type, there is another issue in that not
all controls are required. (There is an associated ASP variable called
"Required" for each control, which is used to change the label CSS class
from balck to red; shown as "LabelClass" in the example above). I'll
need to check for this also somehow. It just gets better and better...
:-)


This is another regular consideration. You could do this by giving
required controls a "required" class name. The class attribute can contain
more than one class name by separating them with whitespace. For example,

function isRequired( e )
{
return( -1 != e.className.indexOf( 'required' ));
}

if(( 'select-one' == t ) && ( 0 == e.selectedIndex ) &&
isRequired( e ))
{
alert( "The " + getLabel( e ) + " menu is a required field." +
"\nPlease choose an option before submitting." );
}

Hope that helps,
Mike
[1] You can bind a label to an element just by including the element
within the label:

<label>A control <input type="text"></label>

However, I've read that IE doesn't support this and the link must be made
with the for and id attributes.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robert | last post by:
did you solve this problem? It seems to be still present here with py2.3.5. Robert -- From: Manish Jethani <manish.j@gmx.net> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;...
3
by: Marcus | last post by:
I'm running into a situation that has me adding a value of "Unknown" to a reference table. I am being pulled between two trains of thought, and was curious to get other's input on in. I give an...
1
by: imw8n4u | last post by:
I need to create combo box that lists unique values from a table and limit the selection to this list. However, I also need to add "UNKNOWN" as a valid selection even though it is not in the table...
2
by: John Baker | last post by:
Hi: I have two systems, one a W98 and the other XP Home. I have Access 2000 installed on both, and have run into a difference in the way the two behave. I have a table on which I wish to reset...
0
by: David P. Donahue | last post by:
I'm using C# to create an ASP .NET website backed by a MySQL database. One of the things I do often on the site is populate a DataList by binding it to a DataSet pulled from the database. However,...
3
by: Ed L. | last post by:
On 7.4.6, is there any problem with defining one column of a view to be a string literal? For example ... $ psql -c "create view fooview as select 'bar' as footype" WARNING: column "footype"...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
9
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined...
0
by: Peter Nofelt | last post by:
Hi all, ISSUE: ==================== In SQL 2005 (sp2) I get the following error when preforming a bulk insert with an associated xml format file: "Could not bulk insert. Unknown version of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.