Connecting Tech Pros Worldwide Help | Site Map

test blank string

js
Guest
 
Posts: n/a
#1: Jul 20 '05
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
Guest
 
Posts: n/a
#2: Jul 20 '05

re: test blank string



"js" <androidsun@yahoo.com> wrote in message
news:23869fd0.0309101547.3f3d871d@posting.google.c om...[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');


john
Guest
 
Posts: n/a
#3: Jul 20 '05

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@yahoo.com> wrote in message
> news:23869fd0.0309101547.3f3d871d@posting.google.c om...[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
}
j s
Guest
 
Posts: n/a
#4: Jul 20 '05

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!
Dr John Stockton
Guest
 
Posts: n/a
#5: Jul 20 '05

re: test blank string


JRS: In article <23869fd0.0309101547.3f3d871d@posting.google.com >, seen
in news:comp.lang.javascript, js <androidsun@yahoo.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.value
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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
asdf asdf
Guest
 
Posts: n/a
#6: Jul 20 '05

re: test blank string


How about

if (typeof yourvar == "undefined" ||
yourvar.toString().trim().length() == 0) return false;
j s
Guest
 
Posts: n/a
#7: Jul 20 '05

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!
Closed Thread