473,574 Members | 3,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17444

"js" <an********@yah oo.com> wrote in message
news:23******** *************** ***@posting.goo gle.com...
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********@yah oo.com> wrote in message
news:23******** *************** ***@posting.goo gle.com...
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********@yah oo.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.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.
Jul 20 '05 #5
How about

if (typeof yourvar == "undefined" ||
yourvar.toStrin g().trim().leng th() == 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
4269
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 while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and...
0
2033
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 TestCase.skipIf(expr, msg): skips if expr is true These can be called either in setUp() or in the test methods. I also added reporting of skipped tests to...
7
3066
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?? thanks!! if (InputForm.username.value == '') alert("username is required");
4
11161
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
2315
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 code, sometimes the Person may not have given us one, both, or either of their addresses. The serialize function will still put in a...
6
2534
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 in is if the user has entered a value into EITHER input box (not left blank). Below is my control code and also my function. I guess my question...
4
2627
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 match is never returned. The string that I give to the regex is one that contains the entire contents of a text file. I'm using the multi-line...
42
2786
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 there is no characters in the variable it does not enter my IF statement like I would think it should. How do I properly check for a
1
2828
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 am working with is using .asp. Their are 30 multiple choice questions. Each will have have 3 or 4 checkboxes where the test taker will choose only...
0
7841
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8271
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7858
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6511
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5654
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.