Groups | Blog | Home
all groups > dotnet jscript > march 2004 >

dotnet jscript : Syntax for accessing controls from javascript using variable names


John Holmes
3/10/2004 9:29:17 AM
I'd like to use a generic validation function that I pass the control name
to and have an alert box come up and then select the text in the text box
and return the focus to the textbox if not valid. It's not working correctly
and I expect my syntax isn't quite right. Please check it out and let me
know what I'm doing wrong. Thanks, John Holmes

function validateNumeric(control)

{

if(isNaN('document.Form1.'+control+'.value'))

{

alert('This field represents a money value and must be numeric.');

'document.Form1.'+control+'.focus()';

'document.Form1.'+control+'.select()';

}


}

gilly3
3/10/2004 9:41:32 AM
"John Holmes" <johnsh@co.skagit.wa.us> wrote in
news:uRp93UsBEHA.1704@TK2MSFTNGP10.phx.gbl:

[quoted text, click to view]

You have your object reference in quotes - this is incorrect. isNaN is
evaluating a string instead of the value of your control.

If you are passing in a string for control, construct your object
reference like this:

document.forms["Form1"].elements["control"]

I'd plop that object reference in a variable and code your function like
this:

function validateNumeric(control)
{
var oControl = document.forms["Form1"].elements["control"];
if(isNaN(oControl.value))
{
alert("This field represents a money value and must be numeric.");
oControl.focus();
oControl.select();
}
}

good luck

John Holmes
3/10/2004 10:37:50 AM
This looks like it should work except for the quotes around the passed
parameter variable "control", won't it take that as a string literal and not
a variable?

I've tried without the quotes and with the quotes and I continue to get
errors. I am confused on how to reference variables correctly in this model.
Thanks, john

[quoted text, click to view]

gilly3
3/10/2004 10:49:43 AM
"John Holmes" <johnsh@co.skagit.wa.us> wrote in
news:OzmGN7sBEHA.3804@TK2MSFTNGP09.phx.gbl:

[quoted text, click to view]

Whoops! Heh. Good catch. Yes, that should be

document.forms["Form1"].elements[control]

You say you've tried it like that and still are having trouble? The
control parameter should be a string (literal or variable) that contains
the name of the form element you want to validate. So, this should work
with your function:

<form name="Form1">
<input name="Field1">
<button onclick="validateNumeric('Field1');">Validate</button>
</form>

bruce barker
3/11/2004 5:48:04 PM
its simpler and cleaner if you just pass the control:

<form name="Form1">
<input name="Field1">
<button onclick="validateNumeric(this);">Validate</button>
</form>

<script>
function validateNumeric(oControl)
{
if(isNaN(oControl.value))
{
alert("This field represents a money value and must be numeric.");
oControl.focus();
oControl.select();
}
}
</script>


-- bruce (sqlwork.com)


[quoted text, click to view]

Randy Webb
3/12/2004 12:25:29 AM
[quoted text, click to view]

"this", in that context, refers to the button, not the input. So the
function is checking to see if the buttons value is a number, if its not
then you get the alert.


--
Randy
Chance Favors The Prepared Mind
bruce barker
3/12/2004 9:24:58 AM
you're right, i should have looked what the code was doing closer. a more
common approach whould be:

<form name="Form1">
<input name="Field1" onblur="validateNumeric(this);">
</form>

<script>
function validateNumeric(oControl)
{
if(isNaN(oControl.value))
{
alert("This field represents a money value and must be numeric.");
oControl.focus();
oControl.select();
}
}
</script>



-- bruce (sqlwork.com)


[quoted text, click to view]

Dave Anderson
3/12/2004 12:20:33 PM
[quoted text, click to view]

An alternative correction to "this" example:

<BUTTON ONCLICK="validateNumeric(this.form.Field1)" ...


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.

AddThis Social Bookmark Button