473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object

I have a field called telephone whose ONBLUR action is to call a javascript
function:
validatePhoneNumber(telephone)

The non-working function is:

function validatePhoneNumber(v)
{
var phone =document.form. + v + .value; //HERE"S WHAT IS
NOT WORKING
var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');
.........//code left out for clarity
document.form. + v + .focus()
}

I want to reuse the function so I need to make it generic. Any ideas?

Many thanks.

Jul 20 '05 #1
7 21314
The first line in your function is not going to work. Pass in the value of
document.form.v.value directly and work with that.

If you need to make it generic across a site, you could pass in the form
element itself which is what I think you are trying to do here. It would
then become...

var phone = document.form.v.value; (Assuming that your form is called form
and that v is one of the named elements within it).

Personally, I would still go with the first option, pass in the value of the
text field to the function and let the function validate the string.

Peter.

"NotGiven" <no****@nonegiven.net> wrote in message
news:Ge*****************@fe05.atl2.webusenet.com.. .
I have a field called telephone whose ONBLUR action is to call a javascript function:
validatePhoneNumber(telephone)

The non-working function is:

function validatePhoneNumber(v)
{
var phone =document.form. + v + .value; //HERE"S WHAT IS
NOT WORKING
var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');
.........//code left out for clarity
document.form. + v + .focus()
}

I want to reuse the function so I need to make it generic. Any ideas?

Many thanks.

Jul 20 '05 #2
NotGiven wrote on 22 jul 2003 in comp.lang.javascript:
I have a field called telephone whose ONBLUR action is to call a
javascript function:
validatePhoneNumber(telephone)

The non-working function is:

function validatePhoneNumber(v)
{
var phone =document.form. + v + .value; //HERE"S WHAT
IS
NOT WORKING
var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');
.........//code left out for clarity
document.form. + v + .focus()
}


var phone =document.form. + v + .value;

this will never work, because the + only works in a string

var phone ="document.form." + v + ".value";

This should work,
because you need a string as you want to strip it in the next line
var stripped = phone.replace(.....

================

document.form. + v + .focus()

Read: <http://www.litotes.demon.co.uk/js_info/sq_brackets.html>

document.form[v].focus()

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #3
On 03.7.22 7:28 PM, NotGiven wrote:
I have a field called telephone whose ONBLUR action is to call a javascript
function:
validatePhoneNumber(telephone)


Can I just add that it's rather bad manners to use onblur() events to check
form fields.

It's like having someone breathing down your neck the whole time. Put this
in an onsubmit() handler instead.

Phil

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 20 '05 #4
"Philip Ronan" <ph***********@virgin.net> wrote in message
news:BB4347AD.16F3E%ph***********@virgin.net...
<snip>
Can I just add that it's rather bad manners to use onblur()
events to check form fields.

It's like having someone breathing down your neck the
whole time. Put this in an onsubmit() handler instead.


I am not sure that validating onBlur itself can be considered bad
manners, it is the possible alert (or similar warning requiring user
interaction) and especially the re-focusing of the form field, trapping
the user into completing that field (if not the entire form) before they
can do anything else, that is objectionable.

Consider, for example, a field for entering your height that is
validated onBlur but all the validation script does is reveal some text
adjacent to the field that reads "Are you really 70 feet tall?" (or
something similar). The user is not prevented from getting on with
whatever they had left the field to do but reviewing the form later they
will be made aware that their entry for that field seems erroneous (and
will probably be rejected by the server if submitted).

Certainly I would prefer to validate a form in its entirety onSubmit as
it is the mechanism provided for that task (at leas I assume that was
the idea behind onSubmit).

Richard.
Jul 20 '05 #5
JRS: In article <BB**************************@virgin.net>, seen in
news:comp.lang.javascript, Philip Ronan <ph***********@virgin.net>
posted at Tue, 22 Jul 2003 19:57:33 :-

Can I just add that it's rather bad manners to use onblur() events to check
form fields.

It's like having someone breathing down your neck the whole time. Put this
in an onsubmit() handler instead.


Forms are not necessarily intended to be Submitted.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
Forms are not necessarily intended to be Submitted.


Forms must have an action attribute which represents how to submit
them. I would say that forms *are* intended (by the specification) to
be submitted, even if they somethimes are not by page authors. Those
page authors are the ones giving us, e.g., 'action="javascript:;"'.

If you need input elements but don't need them to be submitted, then
you don't need a form element at all. After all, it has no visible
effect on the page, and if you don't submit, no semantic effect
either, so it might as well not be there. The only reason to include
it is easy access to the input elements (though document.forms[]).

/L 'Just say no to meaningless forms'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
JRS: In article <1x**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Wed, 23 Jul 2003 20:23:07 :-
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
Forms are not necessarily intended to be Submitted.
Forms must have an action attribute which represents how to submit
them. I would say that forms *are* intended (by the specification) to
be submitted, even if they somethimes are not by page authors. Those
page authors are the ones giving us, e.g., 'action="javascript:;"'.


The author creates the actual forms!
If you need input elements but don't need them to be submitted, then
you don't need a form element at all. After all, it has no visible
effect on the page, and if you don't submit, no semantic effect
either, so it might as well not be there. The only reason to include
it is easy access to the input elements (though document.forms[]).


There has for me been at least one other reason, although I do not
recall what it was.

It may be, as you suggest, to do with element addressability; or it may
be to do with one or more of the checkers/validators that I have used
insisting that input elements must be in Forms (or at least raising a
complaint that I could only resolve that way). It may be something that
I read here.

However, I can remove the form-enclosure from my js-alarm.htm (apart
from only having one form, it's probably structurally typical of my
pages) and adjust the addressing of controls; it still works in MSIE4,
but I cannot readily check that it works elsewhere.

Forms, as I use them, do split the name-space of the page into separate
scopes, which is certainly useful in pages which have a number of
independent scripted-in/out units. Is there another means of doing
that, known to be widely browser-compatible?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8

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

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
7
by: Trvl Orm | last post by:
I am working with 2 frames, Left and Right and the main code is in the left frame, which has been attached. Can someone please help me with this code. I am new to JavaScript and can't figure it...
9
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with...
5
by: DotNetJunkies User | last post by:
1. i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis.......
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.