test blank string

J

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;
 
R

Richard Hockey

js said:
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');
 
J

john

Richard Hockey said:
js said:
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
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
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.
 
J

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

asdf asdf

How about

if (typeof yourvar == "undefined" ||
yourvar.toString().trim().length() == 0) return false;
 
J

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,091
Messages
2,570,604
Members
47,223
Latest member
smithjens316

Latest Threads

Top