How to test an empty input box?

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

    #1

    How to test an empty input box?

    I try to test a field to see if it's empty. But it prints "It is not
    null". Why and how can I fix it?

    <html>
    <head>
    </head>
    <body>
    <input type="text" name="box1">

    <script>
    if (box1.value != null) {document.write ("It is not null")}
    else {document.write ("empty")}
    </script>
    </body>
    </html>
  • Laurent Bugnion, GalaSoft

    #2
    Re: How to test an empty input box?

    Hi,

    chirs wrote:[color=blue]
    > I try to test a field to see if it's empty. But it prints "It is not
    > null". Why and how can I fix it?
    >
    > <html>
    > <head>
    > </head>
    > <body>
    > <input type="text" name="box1">
    >
    > <script>
    > if (box1.value != null) {document.write ("It is not null")}
    > else {document.write ("empty")}
    > </script>
    > </body>
    > </html>[/color]

    null is the value that an object has when it hasn't been initialized, in
    other word when no memory has been allocated for it yet.

    The "value" property exists, however, even if it is empty. To test if a
    field is empty, you must compare it to an empty string:

    if ( document.formNa me.fieldName.va lue == "" )
    {
    //empty
    }

    Laurent
    --
    Laurent Bugnion, GalaSoft
    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch

    Comment

    • Jerry Park

      #3
      Re: How to test an empty input box?

      Laurent Bugnion, GalaSoft wrote:[color=blue]
      > Hi,
      >
      > chirs wrote:
      >[color=green]
      >> I try to test a field to see if it's empty. But it prints "It is not
      >> null". Why and how can I fix it?
      >>
      >> <html>
      >> <head>
      >> </head>
      >> <body>
      >> <input type="text" name="box1">
      >>
      >> <script>
      >> if (box1.value != null) {document.write ("It is not null")}
      >> else {document.write ("empty")}
      >> </script>
      >> </body>
      >> </html>[/color]
      >
      >
      > null is the value that an object has when it hasn't been initialized, in
      > other word when no memory has been allocated for it yet.
      >
      > The "value" property exists, however, even if it is empty. To test if a
      > field is empty, you must compare it to an empty string:
      >
      > if ( document.formNa me.fieldName.va lue == "" )
      > {
      > //empty
      > }
      >
      > Laurent[/color]
      Alternately, you can compare the length of the object's value to 0 .

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: How to test an empty input box?

        Laurent Bugnion, GalaSoft wrote:
        [color=blue]
        > null is the value that an object has when it hasn't been initialized,[/color]

        No, that is `undefined', the primitive sole value of the Undefined type.
        Try

        var a;
        alert(typeof a);

        `null' is a special object literal, the primitive sole value of the
        Null type. It "represents the null, empty, or non-existent reference."
        Try

        alert(typeof null);

        See
        <http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/ident.html#1008 306>

        <http://www.ecma-international.o rg/publications/standards/Ecma-262.htm>
        ECMAScript Edition 3 (on which JavaScript 1.5 is based upon), sections
        4.3.9 to 4.3.12
        [color=blue]
        > in other word when no memory has been allocated for it yet.[/color]

        There is *always* memory allocated for a variable/object/property,
        otherwise it would not be accessible (at least memory for an entry
        in the global hash table must be allocated). AFAIK this is
        independent of the programming language used.


        PointedEars

        Comment

        • Richard Hockey

          #5
          Re: How to test an empty input box?


          "chirs" <yma@kicon.co m> wrote in message
          news:4c22a744.0 310171004.46e19 12f@posting.goo gle.com...[color=blue]
          > I try to test a field to see if it's empty. But it prints "It is not
          > null". Why and how can I fix it?
          >
          > <html>
          > <head>
          > </head>
          > <body>
          > <input type="text" name="box1">
          >
          > <script>
          > if (box1.value != null) {document.write ("It is not null")}
          > else {document.write ("empty")}[/color]

          give this a try

          <script type="text/javascript">
          // build regular expression object to find empty string or any number of
          spaces
          var blankRE=/^\s*$/;

          function CheckEmpty(Text Object)
          {
          if(blankRE.test (TextObject.val ue))
          {
          alert('box1 is empty or contains only spaces.');
          TextObject.focu s();
          }
          }
          </script>

          and in the form

          <input type="text" name="box1" onblur="CheckEm pty(this);">
          [color=blue]
          > </script>
          > </body>
          > </html>[/color]


          Comment

          Working...