Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if number ends in 5 then 2

Status
Not open for further replies.

onressy

Programmer
Joined
Mar 7, 2006
Messages
421
Location
CA
hi, how can i determine the ending number, like
if (ID ends in 5) {
} else if (ID ends in 0) {
} else if (ID ends in 3) {

}

Thanks
 
I forgot to memtion that ID can be of any length
 
Code:
<script type="text/javascript">
  var myval = "This is my string value";
  var char = myval.charAt(myval.length-1);
  switch (char) {
   case 0: alert('Value was 0'); break;
   case 1: alert('Value was 1'); break;
   case 2: alert('Value was 2'); break;
   case 3: alert('Value was 3'); break;
   default: alert('Value was something else')
}
</script>


Google, you're my hero!
 
cheers, that should get me started!
 
I always like to post the regular expression solution when applicable.
Code:
<script type="text/javascript">

var myval = "This is my string value5";
if (/5$/.test(myval)) {
   alert("String ended with a 5");
}
else {
   alert("String did not end with a 5");
}

</script>
The regexp /5$/ breaks down like this:
$ searches for the end of the string. Since the 5 precedes the $, that means that it must be the last character in the string to produce a match and pass the test method. You could replace the number 5 with any character (or set of characters) that you wish to search for.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top