472,131 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

test blank string

js
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;
Jul 20 '05 #1
6 17338

"js" <an********@yahoo.com> wrote in message
news:23**************************@posting.google.c om...
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;


blankRE=/^[\s]+$/

if(A=="" || blankRE.test(A)) then alert('blank string');
Jul 20 '05 #2
"Richard Hockey" <ri***********@dsl.pipex.com> wrote in message news:<3f*********************@news.dial.pipex.com> ...
"js" <an********@yahoo.com> wrote in message
news:23**************************@posting.google.c om...
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;


blankRE=/^[\s]+$/

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


You could also try:

if (A=="" || A==" "){
alert("Enter a value!");
}
else {
//foo
}
Jul 20 '05 #3
j s
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!
Jul 20 '05 #4
JRS: In article <23**************************@posting.google.com >, seen
in news:comp.lang.javascript, js <an********@yahoo.com> posted at Wed,
10 Sep 2003 16:47:34 :-
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;

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.
Jul 20 '05 #5
How about

if (typeof yourvar == "undefined" ||
yourvar.toString().trim().length() == 0) return false;
Jul 20 '05 #6
j s
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!
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Remy Blank | last post: by
4 posts views Thread by Matt | last post: by
6 posts views Thread by EDOnLine | last post: by
42 posts views Thread by =?Utf-8?B?UGxheWE=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.