473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 17439

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
7
by: Matt | last post by:
I want to test the blank data in required field. If the user enter blank data, the following code still not work. But if I test for the length of a string, it doesn't work also, any ideas??...
4
by: Matt | last post by:
I want the javascript to test an alphanumeric (a string contains alphabet or numbers only) string. Should I write a regular expression? What's the best way to do? please help. thanks
5
by: Tappy Tibbons | last post by:
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible? Say for the following...
6
by: EDOnLine | last post by:
New asp.net/webform user question...... I am trying to use the Custom Validator control to check to see if either of two fields on my form have been filled in. At this point all I am interested...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
1
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.