Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / JScript / March 2004

Tip: Looking for answers? Try searching our database.

Syntax for accessing controls from javascript using variable names

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Holmes - 10 Mar 2004 17:29 GMT
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 - 10 Mar 2004 17:41 GMT
> 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

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

-ivan.
John Holmes - 10 Mar 2004 18:37 GMT
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

> > 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
[quoted text clipped - 27 lines]
>
> -ivan.
gilly3 - 10 Mar 2004 18:49 GMT
> 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
[quoted text clipped - 3 lines]
> get errors. I am confused on how to reference variables correctly in
> this model. Thanks, john

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>

-ivan.
bruce barker - 12 Mar 2004 01:48 GMT
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)

> > 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
[quoted text clipped - 19 lines]
>
> -ivan.
Randy Webb - 12 Mar 2004 05:25 GMT
> its simpler and cleaner if you just pass the control:
>
[quoted text clipped - 14 lines]
>  }
> </script>

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

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

bruce barker - 12 Mar 2004 17:24 GMT
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)

> > its simpler and cleaner if you just pass the control:
> >
[quoted text clipped - 23 lines]
> Chance Favors The Prepared Mind
> comp.lang.javascript FAQ - http://jibbering.com/faq/
Dave Anderson - 12 Mar 2004 18:20 GMT
> its simpler and cleaner if you just pass the control:
>
> <form name="Form1">
>      <input name="Field1">
>      <button onclick="validateNumeric(this);">Validate</button>
> </form>

An alternative correction to "this" example:

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

Signature

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.