Passing form reference as variable to function

B

BudAtLitho

This is in my form:
<input type="text" name="form_home"
onblur="f5(this.form,name)"value="<?=$mnhome?>" size="20"
maxlength="20" style="font-family: arial, helvetica, sans-serif;
border-width: 1px; font-size: 8pt; border-top-style: solid; border-
right-style: solid; border-bottom-style: solid; border-left-style:
solid; ">

My function is:
function f5(form,name){//Format phone number
var entry = eval("form."+name+".value")+"";
// alert("entry= "+entry);
var form_name= "form."+name+".value";
// alert(form_name);
if(entry == "") return
if(entry.length < 10){
alert("Please enter complete phone number including area code!");
}
else {//extract numbers from string
var newnumber = "";
var t = entry.length;
for(i=0; i<t; i++){
if(entry.charAt(i)!= "-"&&entry.charAt(i)!= ")"&&entry.charAt(i)!=
"("){
newnumber += entry.charAt(i);
}
}
}
// alert(newnumber);
form.+eval(name)+.value =
"("+newnumber.charAt(0)+newnumber.charAt(1)+newnumber.charAt(2)+")
"+newnumber.charAt(3)+newnumber.charAt(4)+newnumber.charAt(5)+"-"+newnumber.charAt(6)+newnumber.charAt(7)+newnumber.charAt(8)+newnumber.charAt(9);
}
If fails to pass the new value back to the form, as I can't get the
syntax right to refer to the form field name using the variable. I
can get it to work if I replace form.+eval(name)+.value with
form.form_home.value but I want to use this function with many phone
number fields.
Thanks in advance for any help.
 
T

Thomas 'PointedEars' Lahn

BudAtLitho said:
This is in my form:
[...]
My function is:
[...]
If fails to pass the new value back to the form, as I can't get the
syntax right to refer to the form field name using the variable. I
can get it to work if I replace form.+eval(name)+.value with
form.form_home.value but I want to use this function with many phone
number fields.

Sorry, the code you have posted is junk of a magnitude seldom observed here
(and it's not really client-side anyway even though you evidently have a
client-side problem). Few if anyone at all can be bothered to decipher it.
Apply the following to the *complete* *client-side* code (what you get with
View Source, Ctrl+U etc. in the browser), in that order:

<http://validator.w3.org/>
<http://jigsaw.w3.org/css-validator/>
<http://jibbering.com/faq/>, especially <http://jibbering.com/faq/#eval>

Get back if that does not help and follow

<http://jibbering.com/faq/#posting>

then.


PointedEars
 
L

Lasse Reichstein Nielsen

BudAtLitho said:
This is in my form:
<input type="text" name="form_home"
onblur="f5(this.form,name)"value="<?=$mnhome?>" size="20"

There's a missing space after the onblur property.
You pass the form and the name of the form control to the function,
instead of just passing the element itself. It would probably be
better to just write:
onblur="f5(this);"

My function is:
function f5(form,name){//Format phone number
var entry = eval("form."+name+".value")+"";

function f5(element) {
// You receive the element as argument.
var entry = element.value;
// alert("entry= "+entry);
var form_name= "form."+name+".value";

I assume this is just for debugging.
// alert(form_name);
if(entry == "") return
if(entry.length < 10){
alert("Please enter complete phone number including area code!");
}
else {//extract numbers from string
var newnumber = "";
var t = entry.length;
for(i=0; i<t; i++){
if(entry.charAt(i)!= "-"&&entry.charAt(i)!= ")"&&entry.charAt(i)!=
"("){
newnumber += entry.charAt(i);
}
}
}
// alert(newnumber);
form.+eval(name)+.value =

This is meaningless. Just write:
element.value =
"("+newnumber.charAt(0)+newnumber.charAt(1)+newnumber.charAt(2)+")
"+newnumber.charAt(3)+newnumber.charAt(4)+newnumber.charAt(5)+"-"+newnumber.charAt(6)+newnumber.charAt(7)+newnumber.charAt(8)+newnumber.charAt(9);

Shorter as:
"(" + newnumber.substring(0,3) + ")"
+ newnumber.substring(3,6) + "-"
+ newnumber.substring(6,10);
except that you don't know that newnumber has 10 positions (actually,
you are absolutely sure it doesn't).

}
If fails to pass the new value back to the form, as I can't get the
syntax right to refer to the form field name using the variable.

Yes. You don't ever need to use eval.
You could just change it to
form[name].value = ...
but I recommend passing the input element directly as an arugment
instead of passing the form and the element name.

/L
 
B

BudAtLitho

BudAtLitho said:
This is in my form:
<input type="text" name="form_home"
onblur="f5(this.form,name)"value="<?=$mnhome?>" size="20"

There's a missing space after the onblur property.
You pass the form and the name of the form control to the function,
instead of just passing the element itself. It would probably be
better to just write:
�onblur="f5(this);"
My function is:
function f5(form,name){//Format phone number
� �var entry = eval("form."+name+".value")+"";

�function f5(element) {
� �// You receive the element as argument.
� �var entry = element.value;
// alert("entry= "+entry);
� �var form_name= "form."+name+".value";

I assume this is just for debugging.




// alert(form_name);
� � if(entry == "") return
� �if(entry.length < 10){
� � � � � �alert("Please enter complete phone number including area code!");
� �}
� �else {//extract numbers from string
� �var newnumber = "";
� �var t = entry.length;
� �for(i=0; i<t; i++){
� � � � � �if(entry.charAt(i)!= "-"&&entry.charAt(i)!= ")"&&entry.charAt(i)!=
"("){
� � � � � � � � � �newnumber += entry.charAt(i);
� � � � � � � � � �}
� � � � � �}
� �}
// alert(newnumber);
� �form.+eval(name)+.value =

This is meaningless. Just write: �
� � � � element.value =
"("+newnumber.charAt(0)+newnumber.charAt(1)+newnumber.charAt(2)+")
"+newnumber.charAt(3)+newnumber.charAt(4)+newnumber.charAt(5)+"-"+newnumber�.charAt(6)+newnumber.charAt(7)+newnumber.charAt(8)+newnumber.charAt(9);

Shorter as:
� � � � � � � "("+ newnumber.substring(0,3) + ")"
� � � � � � � + newnumber.substring(3,6) + "-"
� � � � � � � + newnumber.substring(6,10);
except that you don't know that newnumber has 10 positions (actually,
you are absolutely sure it doesn't).
}
If fails to pass the new value back to the form, as I can't get the
syntax right to refer to the form field name using the variable.

Yes. You don't ever need to use eval.
You could just change it to
�form[name].value = ...
but I recommend passing the input element directly as an arugment
instead of passing the form and the element name.

/L
--
Lasse Reichstein Holst Nielsen
�'Javascript frameworks is a disruptive technology'- Hide quoted text -

- Show quoted text -

Thanks- works like a charm.

Sorry for the poor code originally posted; I tried a lot of ways to
get the form name to evaluate as I wanted it to so there were a lot of
lines included that were not needed.
One combination I didn't try was form[name].value = always had an
extra period in it. Passing the form field value as an element was
much simpler.
I did search quite a bit on-line for something to point me in the
right direction, but couldn't find anything.

Bud :))
 
D

Dr J R Stockton

In comp.lang.javascript message <3f33356c-d7a2-4131-aada-1fd5b7419c11@o2
6g2000vbd.googlegroups.com>, Tue, 30 Mar 2010 14:39:25, BudAtLitho
This is in my form:
<input type="text" name="form_home"
onblur="f5(this.form,name)"value="<?=$mnhome?>" size="20"
maxlength="20" style="font-family: arial, helvetica, sans-serif;
border-width: 1px; font-size: 8pt; border-top-style: solid; border-
right-style: solid; border-bottom-style: solid; border-left-style:
solid; ">

When posting, you should remove what is superfluous; the style, for
example, there.

My function is:
function f5(form,name){//Format phone number
var entry = eval("form."+name+".value")+"";
// alert("entry= "+entry);
var form_name= "form."+name+".value";
// alert(form_name);
if(entry == "") return
if(entry.length < 10){
alert("Please enter complete phone number including area code!");
}

For News, you should not assume that anything is known about the style
of a phone number except that which is in the applicable ITU standard.
Remember, most of the intelligent people here (and some of the others)
do not live in your own country, whatever that may be.

else {//extract numbers from string
var newnumber = "";
var t = entry.length;
for(i=0; i<t; i++){
if(entry.charAt(i)!= "-"&&entry.charAt(i)!= ")"&&entry.charAt(i)!=
"("){
newnumber += entry.charAt(i);
}
}
}
// alert(newnumber);
form.+eval(name)+.value =
"("+newnumber.charAt(0)+newnumber.charAt(1)+newnumber.charAt(2)+")
"+newnumber.charAt(3)+newnumber.charAt(4)+newnumber.charAt(5)+"-"+newnu
mber.charAt(6)+newnumber.charAt(7)+newnumber.charAt(8)+newnumber.charAt
(9);
}

Code as posted and transmitted should be executable. That means not
allowing your posting agent to wrap lines; do it yourself, according to
syntax.

You seem to be removing some characters from the input, and building a
format from them. That can be better done in general with two uses of
..replace(RegExp) .

The newsgroup FAQ is weak on RegExps. It should for instance say
explicitly, at the start of Section 8, that most string manipulations
are best done with the aid of RegExps.

In 8.1, ignoring optimising for speed or length, String(this) is better
than ("" + this) plus associated comment, because its meaning is clear
without assistance. If that is not so, why ?

My page js-valid.htm will show which characters are matched by \s.
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top