Checking for null value in a field

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Esker

    #1

    Checking for null value in a field

    I've got Javascript in a form that adds up all of the numbers in a
    column of form fields and displays a total. It works great if every
    field has an initial value of 0, but if any of them are null, "NaN"
    displays as the total. I tried to add a test for a null value with
    the intent to skip adding that field to the accumulator for one
    iteration of the "for" loop, but it doesn't work. Can anyone tell me
    what I'm doing wrong? Below is the Javascript:

    function SumFundraisVol( ){
    var TotalSumFundRai sVol = 0;
    var field = 0;
    //Search through the entire array of form elements and
    //locate all fields that include the word "Fundrais_v ol"
    //in their name. When found, add the value of that form
    //element to TotalSumFundRai sVol
    for (var i = 0; i<document.form s[0].elements.lengt h; i++) {
    field = document.forms[0].elements[i].name.indexOf(' Fundrais_vol');
    if (field > -1 && field != null) {
    TotalSumFundRai sVol +=
    eval(document.f orms[0].elements[i].value);
    }
    }
    document.forms[0].Fndraising_Vol _Total.value =
    TotalSumFundRai sVol;
    }
  • G Roydor

    #2
    Re: Checking for null value in a field

    essayez l'opérateur typeof

    Returns a string that identifies the data type of an expression.

    typeof[(]expression[)] ;

    Arguments

    expression
    Required. Any expression.
    Remarks

    The typeof operator returns type information as a string. There are six
    possible values that typeof returns: "number", "string", "boolean",
    "object", "function", and "undefined" .

    The parentheses are optional in the typeof syntax.
    Note : All expressions in JScript .NET have a GetType method. This
    method returns the data type (not a string representing the data type)
    of the expression. The GetType method provides more information than the
    typeof operator.

    G Roydor

    Tom Esker a écrit:[color=blue]
    > I've got Javascript in a form that adds up all of the numbers in a
    > column of form fields and displays a total. It works great if every
    > field has an initial value of 0, but if any of them are null, "NaN"
    > displays as the total. I tried to add a test for a null value with
    > the intent to skip adding that field to the accumulator for one
    > iteration of the "for" loop, but it doesn't work. Can anyone tell me
    > what I'm doing wrong? Below is the Javascript:
    >
    > function SumFundraisVol( ){
    > var TotalSumFundRai sVol = 0;
    > var field = 0;
    > //Search through the entire array of form elements and
    > //locate all fields that include the word "Fundrais_v ol"
    > //in their name. When found, add the value of that form
    > //element to TotalSumFundRai sVol
    > for (var i = 0; i<document.form s[0].elements.lengt h; i++) {
    > field = document.forms[0].elements[i].name.indexOf(' Fundrais_vol');
    > if (field > -1 && field != null) {
    > TotalSumFundRai sVol +=
    > eval(document.f orms[0].elements[i].value);
    > }
    > }
    > document.forms[0].Fndraising_Vol _Total.value =
    > TotalSumFundRai sVol;
    > }[/color]

    Comment

    • Richard Cornford

      #3
      Re: Checking for null value in a field

      G Roydor wrote:
      <snip>[color=blue]
      > typeof[(]expression[)] ;[/color]
      <snip>[color=blue]
      > The parentheses are optional in the typeof syntax.[/color]
      <snip>

      Parentheses are not optional in the "typeof syntax" they are irrelevant
      to it. typeof is a unary operator and acts on a UnaryExpression (ECMA
      262 3rd ed. section 11.4.3). The Parentheses (in that context) are
      Grouping Operators (ECMA 262 3rd ed. section 11.1.6) and surround an
      expression, resulting in an expression. The use of Grouping Operators
      around an expression that typeof operates on, or their omission, makes
      no difference to the typeof operation as the expression is evaluated
      first and typeof only acts on the result of that evaluation.

      Richard.


      Comment

      • Michael Winter

        #4
        Re: Checking for null value in a field

        On 4 Apr 2004 16:49:32 -0700, Tom Esker <tjesker@yahoo. com> wrote:
        [color=blue]
        > I've got Javascript in a form that adds up all of the numbers in a
        > column of form fields and displays a total. It works great if every
        > field has an initial value of 0, but if any of them are null, "NaN"
        > displays as the total. I tried to add a test for a null value with
        > the intent to skip adding that field to the accumulator for one
        > iteration of the "for" loop, but it doesn't work. Can anyone tell me
        > what I'm doing wrong? Below is the Javascript:[/color]

        There are some fundamental problems with this script.

        First of all, if a text field is blank, it's value is not null, but an
        empty string ('').

        field = document.forms[0].elements[i].name.indexOf(' Fundrais_vol');
        if (field > -1 && field != null) {

        Second, the variable, field, has nothing to do with the value of the
        field. It contains the index of the string, 'Fundrais_vol'.

        Thirdly, you don't validate the text fields to ensure that the value
        represent numbers.

        Finally, using eval() to convert a string to a number is a very poor way
        to accomplish that goal.

        Try:

        // By convention, functions start with a lowercase letter
        function sumFundraisVol( )
        {
        var element, form = document.forms[ 0 ],
        collection = f.elements, total = 0, value;

        for( var i = 0, n = collection.leng th; i < n; ++i )
        {
        element = collection[ i ];
        if( -1 != element.name.in dexOf( 'Fundrais_vol' ))
        {
        // Attempt to convert the text value to a number
        value = +element.value;
        // Check if the text did represent a number
        if( !isNaN( value )) {
        total += value;
        }
        }
        }
        form.Fndraising _Vol_Total.valu e = total;
        // Read <URL:http://jibbering.com/faq/#FAQ4_6> for information
        // on formatting numbers as strings.
        }

        [snipped code]

        I haven't tested the code above, so do check it thoroughly before using it.

        Good luck,
        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Dr John Stockton

          #5
          Re: Checking for null value in a field

          JRS: In article <opr50bkitt5vkl cq@news-text.blueyonder .co.uk>, seen in
          news:comp.lang. javascript, Michael Winter <M.Winter@bluey onder.co.invali
          d> posted at Mon, 5 Apr 2004 23:27:43 :
          [color=blue]
          > // Attempt to convert the text value to a number
          > value = +element.value;
          > // Check if the text did represent a number
          > if( !isNaN( value )) {
          > total += value;[/color]

          Valid, of course (FAQ 4.21 mentions that use of +); OTOH, if the OP is
          checking input at all, ISTM wise to check it thoroughly.

          It seems likely that value should be non-negative, and that there is an
          upper limit of credibility, and that any letter, even e or O, in the
          number must be an error.

          if (/^(\d{1,6})$/.test(element.v alue)) total += +RegExp.$1
          or
          OK = /^(\d{1,6})$/.test(element.v alue)
          if (OK) total += +RegExp.$1 ; else alert("Aaarrgh! ")
          or ...

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

          Working...