Or question

T

tshad

I have the following:

if (((strYr.charAt(0)<="0") || (strYr.charAt(0)>="9")) && strYr.length>1)
strYr=strYr.substring(1);

I am trying to take any blanks out of the year.

but if "strYr = 05", it takes out the 0??????

Is there something wrong with this statement?

Thanks,

Tom
 
J

John

tshad said:
I have the following:

if (((strYr.charAt(0)<="0") || (strYr.charAt(0)>="9")) && strYr.length>1)
strYr=strYr.substring(1);

I am trying to take any blanks out of the year.

but if "strYr = 05", it takes out the 0??????

Is there something wrong with this statement?

Hi Tom,

I think this will work for you.

if (((strYr.charAt(0)<"0") || (strYr.charAt(0)>"9")) && strYr.length>1)
strYr=strYr.substring(1);

IMO, this isn't the best way to handle this. Personally, I would do a
replace on a regular expression on non-digits. I think this would work;

cleanString = dirtyString.replace( /\D/g, "");

Good luck,
John MacIntyre
http://www.johnmacintyre.ca
Specializing in; Database, Web-Applications, and Windows Software
 
T

tshad

John said:
Hi Tom,

I think this will work for you.

if (((strYr.charAt(0)<"0") || (strYr.charAt(0)>"9")) && strYr.length>1)
strYr=strYr.substring(1);

I had just figured that out. I looked at this over and over and for some
reason, "= 0" didn't register.

Thanks,

Tom
 
M

Mick White

tshad said:
I have the following:

if (((strYr.charAt(0)<="0") || (strYr.charAt(0)>="9")) && strYr.length>1)
strYr=strYr.substring(1);

I am trying to take any blanks out of the year.

strYr=strYr.replace(/\s/g,"");

I would test strYr further though, so that it falls within an acceptable
range.

but if "strYr = 05", it takes out the 0??????

Is there something wrong with this statement?

strYr.charAt(0)<="0"
strYr.charAt(0)>="9"
Not sure what you are trying to accomplish here.
Mick
 
R

RobG

tshad said:
I have the following:

if (((strYr.charAt(0)<="0") || (strYr.charAt(0)>="9")) && strYr.length>1)
strYr=strYr.substring(1);

I am trying to take any blanks out of the year.

I guess you are getting user-entered text from a text input and
attempting to format it into a 2 digit year. If that is correct, then
as Mick White indicated the above is inadequate.
but if "strYr = 05", it takes out the 0??????

This expression will result in the variable 'strYr' being given the
numeric value '5' and strYr will be a type number. If printed, it will
appear as '5', not '05'.

var y = 05;
alert( y + ' is a ' + typeof y ) // displays '5 is a number'

However, if you give strYr the string '05', then strYr will be a type
string and will print as '05':

var y = '05';
alert( y + ' is a ' + typeof y ) // displays '05 is a string'
Is there something wrong with this statement?

Depends on your point of view. If strYr is a value retrieved from a
text input it will be a string. If your intention is to ensure that
it is a 2 digit number, then firstly:

strYr = strYr.replace('\D'g,'');

will remove all non-digit characters. Subsequently you should test
that the resulting strYr falls within a range that you consider valid.
And note that two-digit years may still be subject to year 2000 issues.

There are some helpful hints on date validation and formatting here:

<URL:http://www.merlyn.demon.co.uk/js-dates.htm>

Having tested that you are happy with whatever was entered, you may
want to format strYr as a 2 digit string for output.

Formatting is usually left until the very end, typically there isn't
much point in bothering about it beforehand. Provided that you ensure
that strYr is at least one digit (0-9), then the following function, if
given a non-negative integer, will return a two digit string:

function towDigit(x){
return '' + ( (x<10)? '0'+ +x : x.match(/\d\d$/) );
}

Numbers from 0 to 9 will have a '0' prepended, numbers greater than 99
will have just the the last two digits returned.

'+x' converts x to a number, removing any existing leading zero.
Prepending a string zero '0' or empty string '' ensures the result is a
string.

Here is a brief implementation:

Enter a year (0 to 99):
<input type="text" onblur="
var y = checkYr(this.value);
alert( ((y)? 'Year is ' + y : 'Year must be 0 to 99') );
">

<script type="text/javascript">
function checkYr(y){
y = y.replace(/\D/g,'');
if ( '' == y ) {
return false;
} else {
// Test for y within suitable range here
// return false if it fails.
}
return towDigit(y);
}

function towDigit(x){
return '' + ( (x<10)? '0'+ +x : x.match(/\d\d$/) );
}
</script>
 
T

tshad

These are all good ideas.

Even after figuring out that I made an error in the ">=" and "<=", I like
some of the other approaches.

Thanks,

Tom
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 1
Jun 2005 01:04:53, seen in RobG
Formatting is usually left until the very end, typically there isn't
much point in bothering about it beforehand. Provided that you ensure
that strYr is at least one digit (0-9), then the following function, if
given a non-negative integer, will return a two digit string:

function towDigit(x){
return '' + ( (x<10)? '0'+ +x : x.match(/\d\d$/) );
}


or return ((x%=100)<10 ? "0" : "") + x
or return String(x+100).match(/\d\d$/)
or return ((x+100)+"").match(/\d\d$/)


ISTM worth distinguishing two cases, typified by getFullYear and
getDate.

To get a two-digit date from getDate one need only prepend a zero of
x<10; x is necessarily in 1..31. Best, then, to use a non-truncating
routine, since if by programming error x is not in that range it would
be better to see the objectionable x in full rather than something
truncated towards plausibility.

But to get a two-digit year from getFullYear, some form of truncation
cannot be avoided. ISTM better to make the truncation straightforward
and manifest as in YY = YYYY%100, after which one can proceed as above.
And even better to use YYYY.

Putting it differently, padding to two digits is quite distinct from
reduction to two digits.
 

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

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top