By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,285 Members | 2,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,285 IT Pros & Developers. It's quick & easy.

Dynamically Change Function Parameters

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.