test blank string

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

    test blank string

    I am using the following script to test if a variable is undefined or
    blank. The alert is not fired, if varA is blank string (ie. "", " ",
    " ", etc). I need to fire the alert even when varA contains blank
    string. How can I do that? Thanks.

    if (varA== undefined || parseInt(varA) == NaN)
    alert("Variable varA is undefined or NaN");
    else
    //do somehting else;
  • Richard Hockey

    #2
    Re: test blank string


    "js" <androidsun@yah oo.com> wrote in message
    news:23869fd0.0 309101547.3f3d8 71d@posting.goo gle.com...[color=blue]
    > I am using the following script to test if a variable is undefined or
    > blank. The alert is not fired, if varA is blank string (ie. "", " ",
    > " ", etc). I need to fire the alert even when varA contains blank
    > string. How can I do that? Thanks.
    >
    > if (varA== undefined || parseInt(varA) == NaN)
    > alert("Variable varA is undefined or NaN");
    > else
    > //do somehting else;[/color]

    blankRE=/^[\s]+$/

    if(A=="" || blankRE.test(A) ) then alert('blank string');


    Comment

    • john

      #3
      Re: test blank string

      "Richard Hockey" <richardhockey@ dsl.pipex.com> wrote in message news:<3f601144$ 0$255$cc9e4d1f@ news.dial.pipex .com>...[color=blue]
      > "js" <androidsun@yah oo.com> wrote in message
      > news:23869fd0.0 309101547.3f3d8 71d@posting.goo gle.com...[color=green]
      > > I am using the following script to test if a variable is undefined or
      > > blank. The alert is not fired, if varA is blank string (ie. "", " ",
      > > " ", etc). I need to fire the alert even when varA contains blank
      > > string. How can I do that? Thanks.
      > >
      > > if (varA== undefined || parseInt(varA) == NaN)
      > > alert("Variable varA is undefined or NaN");
      > > else
      > > //do somehting else;[/color]
      >
      > blankRE=/^[\s]+$/
      >
      > if(A=="" || blankRE.test(A) ) then alert('blank string');[/color]

      You could also try:

      if (A=="" || A==" "){
      alert("Enter a value!");
      }
      else {
      //foo
      }

      Comment

      • j s

        #4
        Re: test blank string

        Hi Richard,
        Thank you for the reply. I tried your solution. /^[\s]+$/ works for
        values that only have 1 or more blank characters. That is " ", " ", "
        ", etc. The regular expression does not work for "". Do you know how
        to make it work for zero length string? Thanks again for your help.


        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Dr John Stockton

          #5
          Re: test blank string

          JRS: In article <23869fd0.03091 01547.3f3d871d@ posting.google. com>, seen
          in news:comp.lang. javascript, js <androidsun@yah oo.com> posted at Wed,
          10 Sep 2003 16:47:34 :-[color=blue]
          >I am using the following script to test if a variable is undefined or
          >blank. The alert is not fired, if varA is blank string (ie. "", " ",
          >" ", etc). I need to fire the alert even when varA contains blank
          >string. How can I do that? Thanks.
          >
          >if (varA== undefined || parseInt(varA) == NaN)
          > alert("Variable varA is undefined or NaN");
          >else
          > //do somehting else;[/color]


          A variable entered by the user is a string. If it seems reasonable to
          test against NaN (for which isNaN() exists), then presumably you want a
          number.

          It is probable that you do not want any arbitrary sort of number - you
          may want non-negative, or not e-format, or not hexadecimal, or not too
          many digits.

          Consider :
          St = form.element.va lue
          OK = /^\d+$/.test(St) // or /^\d{1,5}$/....
          if (OK) varA = +S

          Adjust the RegExp to permit only proper numbers.

          --
          © 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> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          • asdf asdf

            #6
            Re: test blank string

            How about

            if (typeof yourvar == "undefined" ||
            yourvar.toStrin g().trim().leng th() == 0) return false;

            Comment

            • j s

              #7
              Re: test blank string

              Thanks for all your replies. I found the solution to deal with null
              string by chaging Richard Hockey's suggestion,/^[\s]+$/, to /^[\s]*$/.
              This will handel string, null string, number, undefined.

              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              Working...