364,083 Members | 5803 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

extract number from a string - get 13 from 'product13'

chuck
P: n/a
chuck
Hello,
I am trying to figure out how to get a number (always an int) from a
string.

so if i have something like
<input type="text" id="product13" name="product13" onblur="return
somefunc(this);" />

then have a function
somefunc(element){
//extract number from element.name and get 13 in this example
// do other stuff
}

Thanks in advance,
Chuck



Nov 1 '05 #1
Share this Question
Share on Google+
5 Replies


chuck
P: n/a
chuck
chuck wrote:[color=blue]
> Hello,
> I am trying to figure out how to get a number (always an int) from a
> string.
>
> so if i have something like
> <input type="text" id="product13" name="product13" onblur="return
> somefunc(this);" />
>
> then have a function
> somefunc(element){
> //extract number from element.name and get 13 in this example
> // do other stuff
> }
>
> Thanks in advance,
> Chuck[/color]
I found a function to do this, so i thought i would post it
function getNo(stringNo)
{var parsedNo = "";
for(var n=0; n<stringNo.length; n++)
{var i = stringNo.substring(n,n+1);
if(i=="1"||i=="2"||i=="3"||i=="4"||i=="5"||i=="6"| |i=="7"||i=="8"||i=="9"||i==
"0")
parsedNo += i;
}
return parseInt(parsedNo);
}
Nov 1 '05 #2

RobG
P: n/a
RobG
chuck wrote:[color=blue]
> Hello,
> I am trying to figure out how to get a number (always an int) from a
> string.
>
> so if i have something like
> <input type="text" id="product13" name="product13" onblur="return
> somefunc(this);" />
>
> then have a function
> somefunc(element){
> //extract number from element.name and get 13 in this example
> // do other stuff
> }
>[/color]

<input type="text" id="product13" name="product13" onblur="
alert(this.id.replace(/\D/g,''));
">

Replaces all the non-digit characters in the id with an empty string
(essentially nothing) but returns all the digits in the id:

id="product13A13" ==> 1313




If you just want digits at the end:

alert(this.id.match(/\d+$/));

id="product13A13" ==> 13



--
Rob
Nov 1 '05 #3

Danny
P: n/a
Danny



Well:

<input type="text" name="product13" onblur="somefunc(this)/>

function somefunction(el) {
mynumber=Number(el.name.slice(-2));
}

//many ways really

Danny
Nov 1 '05 #4

Zwerfkat
P: n/a
Zwerfkat
"chuck" <nospam@somedomain.com> wrote in message news:11me3clkrdsmscd@corp.supernews.com...[color=blue]
> chuck wrote:[color=green]
>> Hello,
>> I am trying to figure out how to get a number (always an int) from a string.
>>
>> so if i have something like
>> <input type="text" id="product13" name="product13" onblur="return somefunc(this);" />
>>
>> then have a function
>> somefunc(element){
>> //extract number from element.name and get 13 in this example
>> // do other stuff
>> }
>>
>> Thanks in advance,
>> Chuck[/color]
> I found a function to do this, so i thought i would post it
> function getNo(stringNo)
> {var parsedNo = "";
> for(var n=0; n<stringNo.length; n++)
> {var i = stringNo.substring(n,n+1);
> if(i=="1"||i=="2"||i=="3"||i=="4"||i=="5"||i=="6"| |i=="7"||i=="8"||i=="9"||i== "0")
> parsedNo += i;
> }
> return parseInt(parsedNo);
> }[/color]

Or, less complicated:

function somefunc(obj)
{
return obj.id.match(/\d+/)[0];
}

If the number is always at the end of the id, you can better use:

obj.id.match(/\d+$/)[0];



Nov 1 '05 #5

Thomas 'PointedEars' Lahn
P: n/a
Thomas 'PointedEars' Lahn
chuck wrote:
[color=blue]
> <input type="text" id="product13" name="product13" onblur="return
> somefunc(this);" />[/color]

Be sure to declare this as XHTML and to serve as application/xhtml+xml.
`<... />' is most certainly a syntax error in HTML (depending on the
following content). The HTML Compatibility Guidelines of the XHTML 1.0
Specification do take this into account, instead the recommendations
rely merely on error-correction by user agents.

<http://validator.w3.org/>
<http://www.dodabo.de/html+css/tests/shorttag.html>
<http://translate.google.com/translat...ml&langpair=de
en&hl=de&ie=Unknown&oe=ISO-8859-1>


PointedEars
Nov 1 '05 #6

Post your reply

Help answer this question



Didn't find the answer to your JavaScript / Ajax / DHTML question?

You can also browse similar questions: JavaScript / Ajax / DHTML