2

I have a string ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl06_lblCountDown that will come into a javascript function using sender from my asp.net button control...

<asp:Button ID="buttStartTimer" runat="server" CausesValidation="false" OnClientClick="javascript:countdown(this);" Text="Start" />

function test(sender) {

}

The need to get the number directly following ctrl, In the example above it would be 06 (ctrl06_lblCountDown)

How can I extract this number using javascript?

Thanks

Sheri Trager
  • 842
  • 3
  • 13
  • 33
  • Is it always right after the string "ctrl"? – Floris Jan 24 '13 at 17:07
  • possible duplicate of [How to extract a string using Javascript Regex](http://stackoverflow.com/questions/1707299/how-to-extract-a-string-using-javascript-regex) – Lloyd Jan 24 '13 at 17:07
  • 1
    can you provide a few of the different strings so we can see what varies between each one? – dmgig Jan 24 '13 at 17:08
  • I tried both of the answers below but neither returned a value. The ctrl output number could be any number i.e _ctrl45_ They will just be numbers though. – Sheri Trager Jan 24 '13 at 17:30
  • @SheriTrager, I've added a working example to my answer. have a look at this too: http://jsfiddle.net/RXGb2/1/ – CD.. Jan 24 '13 at 17:51

3 Answers3

2
var str = "ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl06_lblCountDown",
    result = str.match(/.*ctrl(\d+).*/)[1];

Working example: http://jsfiddle.net/RXGb2/

CD..
  • 72,281
  • 25
  • 154
  • 163
  • I saw your example and it works great in jsfiddle, but not in my application. I am using asp.net 4, would that matter. I switched to IE and get error... SCRIPT438: Object doesn't support property or method 'match' – Sheri Trager Jan 24 '13 at 17:55
  • Are you sure the `match` is called with a string? – CD.. Jan 24 '13 at 18:00
  • I did check to see what sender was and it was "ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl2_buttStartTime" I reference sender in my asp.net button control using onclientclick="javascript:countdown(this);" I know I must be doing something wrong which is affecting the answers I've been getting. Any ideas? – Sheri Trager Jan 24 '13 at 18:07
  • Ok I just did an alert to see what sender was and it the control. I will try using sender.id – Sheri Trager Jan 24 '13 at 18:09
  • Added id to sender and now it's a string... I guess I've been working on this to long... so Sorry:-(. I now get the number and it works like a champ! Thanks so much for your help and making me think. – Sheri Trager Jan 24 '13 at 18:22
0

You can extract is using regex easily:

var str = "ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl06_lblCountDown";
var num = parseInt(str.match(/_ctrl([\d]*)_/)[1], 10);

Safer way:

var str = "ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl06_lblCountDown";
var parts = str.match(/_ctrl([\d]*)_/), num;
if(parts.length > 1) {
    num = parseInt(parts[1], 10);
}
techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

You could try something like this:

var str = 'ctrl06_lblCountDown',
    numericArray = [],
    numericString,
    num,
    i=0,
    len = 0;


numericArray = str.match(/[0-9]/g);

len = numericArray.length;

numericString = '';
for(i=0; i<len; i++){
    numericString += numericArray[i];

}

num = parseInt(numericString,10);