473,673 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically Change Function Parameters

I'm writing a function to dynamically change a form validation script
depending upon the user's choices.

The form onsubmit is:
onsubmit="write validate(this.s elect.value);re turn document.MM_ret urnValue"

I then have the following function

function writevalidate(s electvalue) {
var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
if (selectvalue==' Person')
{
YY_checkform('f orm1','type','# q','0','Please enter your
Type.','name',' #q','0','Please enter your
name','select', '#q','1','Pleas e select your Status.');
}
else
{
YY_checkform(va r1);
}
}

I'd like to be able to dynamically write my form parameters into the
variable var1 and then insert them into the function like so.

var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
YY_checkform(va r1);

But currently it doesn't work.

How do I do this?

Thanks,
Wayne C.
Jan 20 '06 #1
11 4792
On 2006-01-20, Wayne Cressman <WC*******@etel ligence.com> wrote:
var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
YY_checkform(va r1);

But currently it doesn't work.

How do I do this?


eval("YY_checkf orm("+var1+");" );

Bye.
Jasen
Jan 21 '06 #2
Wayne Cressman wrote:
I'm writing a function to dynamically change a form validation script
depending upon the user's choices.

The form onsubmit is:
onsubmit="write validate(this.s elect.value);re turn document.MM_ret urnValue"

I then have the following function

function writevalidate(s electvalue) {
var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
You can nest single quotes inside doubles and vice versa without
quoting. And don't let posted code auto-wrap, manually wrap it at about
70 characters:

var1 = "'form1','type' ,'#q','0','Plea se enter your "
+ "Type.','select ','#q','1','Ple ase select your Status.'";
But I don't think that is what you want to do anyway.

if (selectvalue==' Person')
{
YY_checkform('f orm1','type','# q','0','Please enter your
Type.','name',' #q','0','Please enter your
name','select', '#q','1','Pleas e select your Status.');
}
else
{
YY_checkform(va r1);
}
}

I'd like to be able to dynamically write my form parameters into the
variable var1 and then insert them into the function like so.
It seems that what you want is to apply different validation rules
depending on what the user has selected or entered. If you describe
what the conditions are, maybe some help can be provided.

Usually validation rules are kept simple and hard-coded since
client-side validation is unreliable. Design the form efficiently and
rely on server-side validation, client-side is just nice for the user
(if done properly, it can be a real pain) and saves some bandwidth.

var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
YY_checkform(va r1);

But currently it doesn't work.


What result would fit your definition of 'work'?

[...]
--
Rob
Jan 23 '06 #3
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etel ligence.com> wrote:
var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
Escaping ' within a "-delimited string literal is not necessary.
YY_checkform(va r1);

But currently it doesn't work.

How do I do this?


eval("YY_checkf orm("+var1+");" );


eval is evil[tm].

YY_checkform.ap ply(this, var1.split(",") );
PointedEars
Jan 23 '06 #4
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etel ligence.com> wrote:
var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
.... eval("YY_checkf orm("+var1+");" );
eval is evil[tm].
Absolutely.
YY_checkform.ap ply(this, var1.split(",") );


This does not give the same result as the expression using eval.
In the eval'ed expression, the arguments will be a list of
"'"-surrounded string literals. In the apply expression, the
arguments are strings values starting and ending with "'".

The split can also fail if one of the strings contain a comma.

For this problem, I would prefer changing var1 to an array to
begin with, instead of a string to be interpreted, or do a
proper parsing of it instead of relying on a simple split.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 23 '06 #5
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etel ligence.com> wrote:
var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'"; ... eval("YY_checkf orm("+var1+");" ); eval is evil[tm].


Absolutely.
YY_checkform.ap ply(this, var1.split(",") );


This does not give the same result as the expression using eval.


It does.
In the eval'ed expression, the arguments will be a list of
"'"-surrounded string literals.
No, they are certainly not.

var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your Type.\'
\'select\',\'#q \',\'1\',\'Plea se select your Status.\'";

is equivalent to

var1 = "'form1','type' ,'#q','0','Plea se enter your
Type.','select' ,'#q','1','Plea se select your Status.'";

and so

eval("YY_checkf orm("+var1+");" );

is evaluated to

YY_checkform('f orm1', 'type', '#q', '0', 'Please enter your Type.',
'select', '#q', '1', 'Please select your Status.');

which can be proven easily:

function YY_checkform()
{
var a = [];
for (var i = 0, len = arguments.lengt h; i < len; i++)
{
a.push(argument s[i]);
}
alert("[" + a.join("\n") + "]");
}
In the apply expression, the arguments are strings values
starting and ending with "'".
Exactly.
The split can also fail if one of the strings contain a comma.
Which cannot be helped, however this was for the specific example.
For this problem, I would prefer changing var1 to an array to
begin with, instead of a string to be interpreted, or do a
proper parsing of it instead of relying on a simple split.


Full ACK.
PointedEars
Jan 23 '06 #6
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Lasse Reichstein Nielsen wrote:
In the eval'ed expression, the arguments will be a list of
"'"-surrounded string literals. .... and so

eval("YY_checkf orm("+var1+");" );

is evaluated to

YY_checkform('f orm1', 'type', '#q', '0', 'Please enter your Type.',
'select', '#q', '1', 'Please select your Status.');


.... where the arguments are exactly a list of string literals,
delimited by "'" (bugger, it's confusing to quote quotations marks,
they're delimited by apostrophes!)
In the apply expression, the arguments are strings values
starting and ending with "'".


Exactly.


I.e., equivalent to:

YY_checkform("' form1'", "'type'", "'#q'", "'0'", "'Please enter your Type.'",
"'select'", "'#q'", "'1'", "'Please select your Status.'");

which is different.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 23 '06 #7
On 2006-01-23, Thomas 'PointedEars' Lahn <Po*********@we b.de> wrote:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etel ligence.com> wrote:
var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
Escaping ' within a "-delimited string literal is not necessary.
It was like that when I found it. [tm]
YY_checkform(va r1);

But currently it doesn't work.

How do I do this?


eval("YY_checkf orm("+var1+");" );


eval is evil[tm].


yeah, but it works.
YY_checkform.ap ply(this, var1.split(",") );
??? don't you need to strip the single quotes from around each string?

and that could get messy real fast if strings fields like

"\"Tom's Diner\",\"Luka" - Susan Vega"

are to be handled.



PointedEars

--

Bye.
Jasen
Jan 24 '06 #8
Jasen Betts wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etel ligence.com> wrote:
var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";
Escaping ' within a "-delimited string literal is not necessary.

[...]
YY_checkform.ap ply(this, var1.split(",") );


??? don't you need to strip the single quotes from around each string?

and that could get messy real fast if strings fields like

"\"Tom's Diner\",\"Luka" - Susan Vega"

^ ^ ^ ^-- beginning of new string literal
| | '-- syntax error
| '----------------.
'----------------------------. |
| |
The string literal that begins here ends there.
are to be handled.


Not quite as messy as you think:

var args = var1.split(/["'],["']/), last = args.length - 1;
args[0] = args[0].replace(/^(['"])/, "");
args[last] = args[last].replace(/(['"])$/, "");

YY_checkform.ap ply(this, args);
PointedEars
Jan 24 '06 #9
On 2006-01-23, Thomas 'PointedEars' Lahn <Po*********@we b.de> wrote:
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etel ligence.com> wrote:
> var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your
> Type.\',\'selec t\',\'#q\',\'1\ ',\'Please select your Status.\'";

...
eval("YY_checkf orm("+var1+");" );
eval is evil[tm].


Absolutely.
YY_checkform.ap ply(this, var1.split(",") );


This does not give the same result as the expression using eval.


It does.
In the eval'ed expression, the arguments will be a list of
"'"-surrounded string literals.


No, they are certainly not.

var1 = "\'form1\',\'ty pe\',\'#q\',\'0 \',\'Please enter your Type.\'
\'select\',\'#q \',\'1\',\'Plea se select your Status.\'";

is equivalent to

var1 = "'form1','type' ,'#q','0','Plea se enter your
Type.','select' ,'#q','1','Plea se select your Status.'";

and so

eval("YY_checkf orm("+var1+");" );

is evaluated to

YY_checkform('f orm1', 'type', '#q', '0', 'Please enter your Type.',
'select', '#q', '1', 'Please select your Status.');

which can be proven easily:

function YY_checkform()
{
var a = [];
for (var i = 0, len = arguments.lengt h; i < len; i++)
{
a.push(argument s[i]);
}
alert("[" + a.join("\n") + "]");
}


try this one.

function YY_checkform(a, b,c,d,e,f,g,h,i )
{
var z=a+b+c+d+e+f+g +h+i;
document.write( z.length);
}
document.write( "<br> my way ");
eval("YY_checkf orm("+var1+");" );
document.write( "<br> your way ");
YY_checkform.ap ply(this, var1.split(",") );

which produces:

my way 70
your way 88
In the apply expression, the arguments are strings values
starting and ending with "'".


Exactly.


which is a different result.
The split can also fail if one of the strings contain a comma.


Which cannot be helped, however this was for the specific example.


if the specific example is all that needs to be staisfied the best solution
would be this:

YY_checkform('f orm1','type','# q','0','Please enter your Type.',
'select','#q',' 1','Please select your Status.');
:)
For this problem, I would prefer changing var1 to an array to
begin with, instead of a string to be interpreted, or do a
proper parsing of it instead of relying on a simple split.


Full ACK.


yeah, that sounds like the best solution.

Bye.
Jasen
Jan 24 '06 #10

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

Similar topics

2
2101
by: Chris Haynes | last post by:
Hello all, I have a structure: typedef struct UVstruct { float u, v; } uv; Inside a function (A) i declare a pointer to an instance of this structure:
12
2800
by: Joel | last post by:
Hi all, Forgive me if I've expressed the subject line ill. What I'm trying to do is to call a c++ function given the following: a. A function name. This would be used to fetch a list of function descriptors for the overloaded functions of that name. A function descriptor would contain the address of the function to be called, and a description of the parameters that it must take. b. A list of parameters. This would be compared to the...
2
1829
by: Joe | last post by:
I have 3 functions: ClientInfoA is doing something ClientInfoB is doing something SelectFunction2Run is a function to determine which function needed to run based on the value of the variable Method2Run. If the clientType is A, it would run ClientInfoA function. If it is clientType B, it would run the ClientInfoB function. Based on the value of Method2Run, how would I run the function dynamically? I know that there are many ways not to...
3
12481
by: N. Demos | last post by:
How do you dynamically assign a function to an element's event with specific parameters? I know that the code is different for MSIE and Mozilla, and need to know how to do this for both. I have the following event handler functions defined and need to assign them to table elements (<TD>) created dynamically in another function. function handleMouseOver(elt, cssClass, strURL)
11
2287
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .... py> C().spam(3) spam spam spam
2
3737
by: Jose Suero | last post by:
Hi all I have a dynamically created button, I can add an event handler with: AddHandler button.click, AddressOf static_function This works great, but what I need is to create a function that takes the control, the event and the function as parameters, something like: function addevent(control as object, event as string, functionname as string)
1
7006
by: tfsmag | last post by:
Hello, I have a function that returns a dynamically created gridview. This works fine, however it does not seem to be able to maintain state when adding sorting or paging to the gridview. Does anyone have any idea how to get this to work? below is the code. Please bear in mind that the function is actually located in a seperate class file from the page that actually returns the grid. ---code for function that returns the grid, this is...
0
952
by: kjvspam | last post by:
I have a bunch of reports that are defined as metadata in a database. Each report has an ID and a collection of filters (e.g. filter the report where X is between 5 and 8). I need to expose the reports through a web service. I could write a single web method that takes in a report ID and an ArrayList of parameters, and returns the output for the requested report. That would work. But I think the generic method above is too generic. I...
0
8509
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8855
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8652
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7487
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5729
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4254
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2853
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2104
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.