473,513 Members | 3,621 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="writevalidate(this.select.value);return document.MM_returnValue"

I then have the following function

function writevalidate(selectvalue) {
var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";
if (selectvalue=='Person')
{
YY_checkform('form1','type','#q','0','Please enter your
Type.','name','#q','0','Please enter your
name','select','#q','1','Please select your Status.');
}
else
{
YY_checkform(var1);
}
}

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\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";
YY_checkform(var1);

But currently it doesn't work.

How do I do this?

Thanks,
Wayne C.
Jan 20 '06 #1
11 4775
On 2006-01-20, Wayne Cressman <WC*******@etelligence.com> wrote:
var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";
YY_checkform(var1);

But currently it doesn't work.

How do I do this?


eval("YY_checkform("+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="writevalidate(this.select.value);return document.MM_returnValue"

I then have the following function

function writevalidate(selectvalue) {
var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#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','Please enter your "
+ "Type.','select','#q','1','Please select your Status.'";
But I don't think that is what you want to do anyway.

if (selectvalue=='Person')
{
YY_checkform('form1','type','#q','0','Please enter your
Type.','name','#q','0','Please enter your
name','select','#q','1','Please select your Status.');
}
else
{
YY_checkform(var1);
}
}

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\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";
YY_checkform(var1);

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*******@etelligence.com> wrote:
var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";
Escaping ' within a "-delimited string literal is not necessary.
YY_checkform(var1);

But currently it doesn't work.

How do I do this?


eval("YY_checkform("+var1+");");


eval is evil[tm].

YY_checkform.apply(this, var1.split(","));
PointedEars
Jan 23 '06 #4
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etelligence.com> wrote:
var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";
.... eval("YY_checkform("+var1+");");
eval is evil[tm].
Absolutely.
YY_checkform.apply(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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 23 '06 #5
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etelligence.com> wrote:
var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'"; ... eval("YY_checkform("+var1+");"); eval is evil[tm].


Absolutely.
YY_checkform.apply(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\',\'type\',\'#q\',\'0\',\'Please enter your Type.\'
\'select\',\'#q\',\'1\',\'Please select your Status.\'";

is equivalent to

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

and so

eval("YY_checkform("+var1+");");

is evaluated to

YY_checkform('form1', '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.length; i < len; i++)
{
a.push(arguments[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*********@web.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_checkform("+var1+");");

is evaluated to

YY_checkform('form1', '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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 23 '06 #7
On 2006-01-23, Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etelligence.com> wrote:
var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#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(var1);

But currently it doesn't work.

How do I do this?


eval("YY_checkform("+var1+");");


eval is evil[tm].


yeah, but it works.
YY_checkform.apply(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*******@etelligence.com> wrote:
var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";
Escaping ' within a "-delimited string literal is not necessary.

[...]
YY_checkform.apply(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.apply(this, args);
PointedEars
Jan 24 '06 #9
On 2006-01-23, Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etelligence.com> wrote:
> var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
> Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";

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


Absolutely.
YY_checkform.apply(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\',\'type\',\'#q\',\'0\',\'Please enter your Type.\'
\'select\',\'#q\',\'1\',\'Please select your Status.\'";

is equivalent to

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

and so

eval("YY_checkform("+var1+");");

is evaluated to

YY_checkform('form1', '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.length; i < len; i++)
{
a.push(arguments[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_checkform("+var1+");");
document.write("<br> your way ");
YY_checkform.apply(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('form1','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
On 2006-01-24, Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
Jasen Betts wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Jasen Betts wrote:
On 2006-01-20, Wayne Cressman <WC*******@etelligence.com> wrote:
> var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
> Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";

Escaping ' within a "-delimited string literal is not necessary. [...]
YY_checkform.apply(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.apply(this, args);


no.

var="\" Comma ',' is an \\\"error\\\"\"";
Bye.
Jasen
Jan 25 '06 #11
Jasen Betts wrote:
On 2006-01-24, Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
Jasen Betts wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Jasen Betts wrote:
> On 2006-01-20, Wayne Cressman <WC*******@etelligence.com> wrote:
>> var1 = "\'form1\',\'type\',\'#q\',\'0\',\'Please enter your
>> Type.\',\'select\',\'#q\',\'1\',\'Please select your Status.\'";

Escaping ' within a "-delimited string literal is not necessary.
[...]
YY_checkform.apply(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.apply(this, args);


no.

var="\" Comma ',' is an \\\"error\\\"\"";


Are you trying to make the point that my solution is not a general one?
If yes, I already knew that. However, it works without evil[tm] eval()
for the OP's input, and it is not quite as messy as you thought it to be.
PointedEars
Jan 25 '06 #12

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

Similar topics

2
2089
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
2784
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...
2
1808
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...
3
12463
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. ...
11
2270
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
3716
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...
1
7000
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...
0
945
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...
0
7269
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...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7123
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...
0
5701
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...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3248
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...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
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
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.