Dynamically Change Function Parameters

W

Wayne Cressman

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.
 
R

RobG

Wayne said:
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'?

[...]
 
T

Thomas 'PointedEars' Lahn

Escaping ' within a "-delimited string literal is not necessary.
eval("YY_checkform("+var1+");");

eval is evil[tm].

YY_checkform.apply(this, var1.split(","));


PointedEars
 
L

Lasse Reichstein Nielsen

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
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Thomas 'PointedEars' Lahn said:
Jasen said:
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);
}
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
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Lasse Reichstein Nielsen wrote: ....
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!)

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
 
T

Thomas 'PointedEars' Lahn

Jasen said:
Jasen said:
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
 
T

Thomas 'PointedEars' Lahn

Jasen said:
Jasen said:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Jasen Betts 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top