3

I'm trying to get the row for controls that can have ids like the one below (generated by asp.net webform).

id="contentMain__lvTSEntry__tbxMinute_1"

I'm trying to see if I can capture just the digit, so I'm using the below javascript.

if (e.id.indexOf("_tbxMinute") != -1) {                        
    if (parseInt(e.value) > 59) {
       var cid = e.id;
       var patt = '\d+'
       var test = cid.match(patt)
       alert(test)
      } 

So far I'm getting null. How do I get digit from the control id?

Thanks for helping

2
  • e.id.indexOf("_tbxMinute") or e.id.indexOf("_tbxHour")? Commented Dec 20, 2012 at 16:11
  • @Mario, I made a mistake that I just corrected Commented Dec 20, 2012 at 16:13

5 Answers 5

3

If you change your pattern string from var patt = '\d+' to var patt = '\\d+' it should work (My try at jsfiddle did it)

Sign up to request clarification or add additional context in comments.

Comments

0

You could use something like this:

/contentMain__lvTSEntry__tbxHour_(\d+)/

It captures the digit:

> var regex = /contentMain__lvTSEntry__tbxHour_(\d+)/;
> regex.exec("contentMain__lvTSEntry__tbxHour_1")[1]
"1"

Alternatively, you could just use /(\d+)/ as the entire regex.

Comments

0

You can use this code to remove any non digit characters

"contentMain__lvTSEntry__tbxHour_1".replace(/\D/g,'')

Comments

0

You can use this function, that returns all numbers in array:

function returnNumbers(string){
    return string.match(/\d+/g);
}

Examples:

> returnNumbers('123asd123')
["123", "123"]

> returnNumbers('contentMain__lvTSEntry__tbxMinute_1')
["1"]

Comments

0

If you are using patt as a regular expression, you should declare it as a RegExp type:

var patt = /\d+/;

patt instanceof RegExp; // true

var patt = '\d+' implies that patt is d+, because \d is translated to d as \ is an escape character. Then, match will receive d+ instead of \d+ as argument.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.