Assign value to control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ckerns@freuse.net

    #1

    Assign value to control

    I want to loop thru an array of controls,(39 of them...defaults = 0).
    If value is null or non-numeric I want to assign the value of "0".

    rowString = "L411" //conrol name

    if (isNaN(eval ("document.form s[0]."+rowString+". value")) == true )
    {
    //this alert works if the value is a letter,i.e,"a"
    alert("You have entered an non-numeric value.\nEnter a number
    in the appropriate box.");

    //If I leave this in the code dies
    //document.forms[0]." + rowString + ".value = 0;
    }



    I've played with this, but no luck.




    if (isNaN(eval ("document.form s[0]."+rowString+". value")) == true ||
    isEmpty(eval ("document.form s[0]."+rowString+". value")) == true )
    {
    //this alert works if the value is a letter,i.e,"a"
    alert("You have entered an non-numeric value.\nEnter a number
    in the appropriate box.");

    //If I leave this in the code dies
    //document.forms[0]." + rowString + ".value = 0;
    }

    Any hints here?


    Thansk

    ck


  • Evertjan.

    #2
    Re: Assign value to control

    wrote on 15 apr 2005 in comp.lang.javas cript:
    [color=blue]
    > if (isNaN(eval ("document.form s[0]."+rowString+". value")) == true )[/color]

    If you follow this NG for a while, you will notice some rules that can help
    you:

    1 eval() is evil, you do not need it

    2 testing in an if clause if something is true
    is the same as just using the something.

    if (isNaN(document .forms[0].elements[rowString].value))
    alert("not a number")


    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)

    Comment

    • RobB

      #3
      Re: Assign value to control

      cke...@freuse.n et wrote:[color=blue]
      > I want to loop thru an array of controls,(39 of them...defaults = 0).
      > If value is null or non-numeric I want to assign the value of "0".
      >
      > rowString = "L411" //conrol name
      >
      > if (isNaN(eval ("document.form s[0]."+rowString+". value")) == true )
      > {
      > //this alert works if the value is a letter,i.e,"a"
      > alert("You have entered an non-numeric value.\nEnter a number
      > in the appropriate box.");
      >
      > //If I leave this in the code dies[/color]

      ------> (probably because I forgot the eval()!)
      [color=blue]
      > //document.forms[0]." + rowString + ".value = 0;
      > }
      >
      >
      >
      > I've played with this, but no luck.
      >
      >
      >
      >
      > if (isNaN(eval ("document.form s[0]."+rowString+". value")) == true ||
      > isEmpty(eval ("document.form s[0]."+rowString+". value")) == true )
      > {
      > //this alert works if the value is a letter,i.e,"a"
      > alert("You have entered an non-numeric value.\nEnter a number
      > in the appropriate box.");
      >
      > //If I leave this in the code dies
      > //document.forms[0]." + rowString + ".value = 0;
      > }
      >
      > Any hints here?
      >
      >
      > Thansk
      >
      > ck[/color]

      What does 'an array of controls' mean?

      You can put the names in a (real) array:

      var cnames = ['L411' , 'M58' , 'j54' ....]

      ....and loop *that*...

      var el, els = document.forms[0].elements;
      for (var i = 0, l = cnames.length; i < l; ++i)
      {
      if ((el = els[cnames[i]])
      && /^\D*$/.test(el.value) )
      el.value = '0';
      }

      Comment

      • RobB

        #4
        Re: Assign value to control

        Oops...

        if ((el = els[cnames[i]])
        && !/^\d+$/.test(el.value) )

        [Friday]

        Comment

        • ckerns@furssse.net

          #5
          Re: Assign value to control

          I am newly.

          Why not eval...any links?

          I tried code, but no luck...

          thx

          On 15 Apr 2005 20:40:23 GMT, "Evertjan."
          <exjxw.hannivoo rt@interxnl.net > wrote:
          [color=blue]
          > wrote on 15 apr 2005 in comp.lang.javas cript:
          >[color=green]
          >> if (isNaN(eval ("document.form s[0]."+rowString+". value")) == true )[/color]
          >
          >If you follow this NG for a while, you will notice some rules that can help
          >you:
          >
          >1 eval() is evil, you do not need it
          >
          >2 testing in an if clause if something is true
          >is the same as just using the something.
          >
          >if (isNaN(document .forms[0].elements[rowString].value))
          > alert("not a number")[/color]

          Comment

          • Evertjan.

            #6
            Re: Assign value to control

            wrote on 16 apr 2005 in comp.lang.javas cript:[color=blue]
            > On 15 Apr 2005 20:40:23 GMT, "Evertjan."
            > <exjxw.hannivoo rt@interxnl.net > wrote:
            >[color=green]
            >> wrote on 15 apr 2005 in comp.lang.javas cript:
            >>[color=darkred]
            >>> if (isNaN(eval ("document.form s[0]."+rowString+". value")) == true )[/color]
            >>
            >>If you follow this NG for a while, you will notice some rules that can
            >>help you:
            >>
            >>1 eval() is evil, you do not need it
            >>
            >>2 testing in an if clause if something is true
            >>is the same as just using the something.
            >>
            >>if (isNaN(document .forms[0].elements[rowString].value))
            >> alert("not a number")[/color][/color]

            [please do not toppost. Topposting corrected]
            [color=blue]
            > I am newly.
            >
            > Why not eval...any links?[/color]

            Sure, read the archive of this NG

            <http://groups-beta.google.com/groups?q=eval+i s+evil&safe=off >

            3410 hits
            [color=blue]
            > I tried code, but no luck...[/color]

            Programming is not a question of luck,
            but of basic knowledge, testing and debugging.

            Try this file:

            ========= test.html =========


            <form>
            row 0: <input value="3"><br>
            row 1: <input value="blah">
            </form>

            <script type='text/javascript'>

            var rowString=0

            if(isNaN(docume nt.forms[0].elements[rowString].value))
            alert("row " + rowString + " value is not a number")
            else
            alert("row " + rowString + " value is is a number")

            rowString=1

            if(isNaN(docume nt.forms[0].elements[rowString].value))
            alert("row " + rowString + " value is not a number")
            else
            alert("row " + rowString + " value is is a number")

            </script>

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

            Communication would be easier if you give a [nick]name, btw.

            --
            Evertjan.
            The Netherlands.
            (Replace all crosses with dots in my emailaddress)

            Comment

            • ckerns@refuse.net

              #7
              Re: Assign value to control

              On 15 Apr 2005 15:13:44 -0700, "RobB" <ferndoc9@hotma il.com> wrote:
              [color=blue]
              >Oops...
              >
              > if ((el = els[cnames[i]])[/color]
              I tried hard, but I got lost here. I googled special charcter pages,
              but I'm still unsure what this means.

              I am sorry because I know you put effort into this.

              I'm only 14, so don't get mad at me...I'm trying real hard.


              ck[color=blue]
              > && !/^\d+$/.test(el.value) )
              >
              >[Friday][/color]

              Comment

              • Dr John Stockton

                #8
                Re: Assign value to control

                JRS: In article <Xns9639E6A6465 7Deejj99@194.10 9.133.29>, dated Fri, 15
                Apr 2005 20:40:23, seen in news:comp.lang. javascript, Evertjan.
                <exjxw.hannivoo rt@interxnl.net > posted :[color=blue]
                >
                >if (isNaN(document .forms[0].elements[rowString].value))
                > alert("not a number")[/color]

                Since for most applications the set of allowable and reasonable inputs
                is much smaller than the set of all inputs that isNaN will accept (the
                latter is in fact infinite; or, rather, bounded by outside
                considerations) , ISTM that the above test is weaker than might be
                appropriate.

                if (!/\d+/.test(document. forms[0].elements[rowString].value))
                alert('not good') // or /\d+\.\d\d/

                might be a better starting-point.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • Evertjan.

                  #9
                  Re: Assign value to control

                  Dr John Stockton wrote on 16 apr 2005 in comp.lang.javas cript:
                  [color=blue]
                  > JRS: In article <Xns9639E6A6465 7Deejj99@194.10 9.133.29>, dated Fri, 15
                  > Apr 2005 20:40:23, seen in news:comp.lang. javascript, Evertjan.
                  > <exjxw.hannivoo rt@interxnl.net > posted :[color=green]
                  >>
                  >>if (isNaN(document .forms[0].elements[rowString].value))
                  >> alert("not a number")[/color]
                  >
                  > Since for most applications the set of allowable and reasonable inputs
                  > is much smaller than the set of all inputs that isNaN will accept (the
                  > latter is in fact infinite; or, rather, bounded by outside
                  > considerations) , ISTM that the above test is weaker than might be
                  > appropriate.
                  >
                  > if (!/\d+/.test(document. forms[0].elements[rowString].value))
                  > alert('not good') // or /\d+\.\d\d/
                  >
                  > might be a better starting-point.[/color]

                  You are right of course, John, I just wanted to show the right way of:

                  document.forms[0].elements[rowString].value

                  in answer on a "Does not work" statement.


                  --
                  Evertjan.
                  The Netherlands.
                  (Replace all crosses with dots in my emailaddress)

                  Comment

                  • Richard Cornford

                    #10
                    Re: Assign value to control

                    ckerns@refuse.n et wrote:[color=blue]
                    > RobB wrote:[/color]
                    <snip>[color=blue][color=green]
                    >> if ((el = els[cnames[i]])[/color]
                    > I tried hard,[/color]

                    What did you try?
                    [color=blue]
                    > but I got lost here.[/color]

                    What do you mean by 'got lost'. Generally, the more effort you put in to
                    asking questions the better the answers you get will be. And RodB asked
                    you a question, where is its answer?
                    [color=blue]
                    > I googled special charcter pages,[/color]

                    Why?
                    [color=blue]
                    > but I'm still unsure what this means.[/color]

                    You are responding to a quote of half of the opening part of an - if -
                    statement. The full statement (without contents) was:-

                    You are responding to a quote of half of the opening part of an - if -
                    statement. The full statement was:-

                    if ((el = els[cnames[i]]) && /^\D*$/.test(el.value) )
                    el.value = '0';

                    General programming advice (and the well known JSLINT) would insist that
                    the statement following the condition expression in an - if - statement
                    should always, and only, be a block statement (which would then contain
                    the statement that would otherwise follow the - if - expression). The
                    language doesn't require this but it avoids errors when you find you
                    need to ad another statement. Even experienced programmers trip up when
                    doing that so insisting on the block statement is a worthwhile habit to
                    acquire. So the formatting would be:-

                    if ((el = els[cnames[i]]) && /^\D*$/.test(el.value) ){
                    el.value = '0';
                    }

                    The ECMA 262 specified production rule for an if statement is:-

                    if ( Expression ) Statement

                    In the case of this - if - statement the expression is a logical AND
                    expression. Logical AND is a binary expression, in that its operator -
                    && - has an operand to its left and its right (two operands, hence
                    binary)

                    The operand on the left-hand side is evaluated and the result of that
                    evaluation is type-converted to a boolean (true or false) value. If it
                    is false then the result of the logical AND expression is the evaluated
                    result of the left-hand side expression (not the value following its
                    type-conversion to boolean). If the result is false then the right hand
                    side is evaluated and that becomes the result of the logical AND
                    expression.

                    The - if - statement decides whether to execute the statement it
                    contains based on the value of the Expression, that expression is the
                    logical AND expression. The result of that expression is type-converted
                    to boolean and if the result of that is true then the contained
                    statement is executed, otherwise it is skipped.

                    The left hand side of the logical AND expression is the expression:-

                    (el = els[cnames[i]])

                    The right hand side of the logical AND expression is the expression:-

                    /^\D$/.test(el.value)

                    This right-hand side expression is a method call on an object. The
                    object is a regular expression literal. It is a regular expression
                    object that will math zero or more occurrences of non-decimal digit
                    characters making up the entire contents of a string:-

                    ^ <-- the start of a string
                    /D <-- non-decimal digit character
                    * <-- zero or more occurrences
                    $ <-- the end of a string

                    Regular expressions are objects and objects may have methods; functions
                    that may be executed in association with the specific instance of the
                    object in question. The Regular expression object has a method called -
                    test - and the dot-notation property accessor - /^\D*/.test - refers to
                    that method of the object (in a way that retains the association of the
                    function with the object.

                    Following a property accessor that refers to a function with an
                    expression that represents an arguments list (which may be empty) is a
                    call to that function. The function is executed, and as the association
                    with the object exist the function is executed as a method of that
                    object.

                    The test function takes a string argument (or type-converts its argument
                    into a string if it is not of string primitive type to start with). If
                    the regular expression matches the string argument then the method call
                    returns the boolean value true, otherwise it returns false. Thus the
                    right hand side of the logical AND expression will evaluate as boolean
                    true or false, depending on the argument.

                    The argument is another dot notation property accessor - el.value -. The
                    identifier - el - is resolved against the scope chain and a value is the
                    result. That value is then type-converted into an object, which does not
                    involve doing anything here, as it would be an object to start with. (By
                    'an object' I mean a reference to an object. That is; the value of -
                    el - is a reference to an object). The identifier after the dot is then
                    resolved as a named property of that object. the result of the property
                    accessor is the value of that named property of the object.

                    As - el - is expected to refer to an INPUT element in the DOM and INPUT
                    elements have 'value' properties that refer to the string value of the
                    INPUT element, the dot notation property accessor is resolved as that
                    string value. That string value becomes the argument to the - test -
                    method call on the regular expression object, which will return boolean
                    true or false.

                    The left had side of the logical AND expression:-

                    (el = els[cnames[i]])

                    - is an assignment expression. The right-hand side of the assignment
                    operator - = - is evaluated and its value is assigned to the
                    identifier - el - (or, in reality, to the property of an object on the
                    scope chain with the name '"el") on the left-hand side of the
                    assignment.

                    You will probably have noticed that expressions tend to be made up of
                    simpler expressions, and that expressions result in values. The
                    assignment expression, while doing something specific, also results in a
                    value. The value of the entire assignment expression is the value
                    resulting from the evaluation of the right-hand side, as assigned to the
                    left-hand side.

                    The right hand side of the assignment expression is a bracket notation
                    property accessor. Bracket notation property accessors are just like
                    dot-notation property accessors in terms of what they do, but they are
                    more flexible because instead of stating an identifier as the property
                    name after the dot they may use any string value (or any expression that
                    will result in a string value, or can be type-converted into a string).

                    (Bracket notation also allows property names to consist of any character
                    sequence rather than the restricted set that qualify as an Identifier.
                    This is particularly important in the case of array indexes as
                    Identifiers may not start with a decimal digit, and array indexes may
                    only contain decimal digits)

                    Thus:-

                    document.body

                    - is the equivalent of:-

                    document["body"]

                    In the property accessor - els[cnames[i]] - the expression that will
                    result in the name that is to be resolved as a named property of the -
                    els - object, is the expression - cnames[i] -. That expression is
                    another bracket notation property accessor, with the Identifier - i -
                    being resolved to provide the name for a property of the object referred
                    to by the identifier - cnames -.

                    cnames - is an array of strings and - i - is an integer value so -
                    cnames[i] - results in one of the strings in the - cnames - array
                    (assuming the integer is within the appropriate range). That string is
                    then used as the name of a property of the - els - object, and the
                    result of the expression is whatever value is held by whichever name is
                    the result of the indexed reference to the array of strings.

                    els - has been assigned a reference to the - elements - collection of
                    the first FORM element in the document. The - cnames - array is expected
                    to hold a list of all of the names or form controls within that form. So
                    the right hand side of the assignment expression is expected to result
                    in a reference to one of the form control objects. It is that reference
                    that is assigned to the - el - identifier. The result of the entire
                    assignment expression is also the reference to that object, which is
                    then type-converted into a boolean value to determine the behaviour of
                    the logical AND expression.

                    The advantage of this is that if the right hand side of the assignment
                    expression doe not resolve as a reference to a form control then the
                    result of the assignment expression will be the value - undefined -
                    (which, potentially confusingly, is a distinct and specific primitive
                    value in javascript). When the logical AND expression evaluates its
                    left-hand side, and the assignment expression results in - undefined -
                    then the type-converted boolean value is false, so the logical AND
                    expression returns false (and its right-hand side is not evaluated).
                    This short circuiting in the logical AND expression mans that its right
                    hand side will only be executed if - el - is assigned a value that
                    type-converts to boolean true, which all object references do, and
                    avoids that right hand side erroring (as it must if it tried to evaluate
                    the property accessor - el.value - when - el - had been assigned -
                    undefined -).
                    [color=blue]
                    > I am sorry because I know you put effort into this.
                    >
                    > I'm only 14, so don't get mad at me...I'm trying real hard.[/color]
                    <snip>

                    Usenet can be a good pace to get help with this sort of subject, if you
                    are willing to put some effort in for yourself. But you need to play 'by
                    the rules' (they aren't really rules, just long established
                    conventions), for which you need to familiarise yourself with the rules.
                    Reading this group's FAQ will help a great deal in achieving that.

                    <URL: http://www.jibbering.com/faq/ >

                    Richard.


                    Comment

                    Working...