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