HighDots Forums  

Substring

Javascript JavaScript language (comp.lang.javascript)


Discuss Substring in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Daviso
 
Posts: n/a

Default Substring - 02-03-2006 , 06:36 AM






Hi.

I hava an input like this
<td><input name=numberA> </td>

and a button with a javascript function in onclick event.

I want to control that the first element of the value I have introduced in
numberA will be different and make a conditional

for example
43543, I want to control the first digit (4)
5433, I watn to control the first digit(5)

I'm using "substring" but it seems doesn't work
I have:
If (document.F.numberA.value.substring(1,1) == "4")
or
If (document.F.numberA.subtring(1,1) == "4")

but no way. How can I do this????
Thanks



Reply With Quote
  #2  
Old   
Daviso
 
Posts: n/a

Default Re: Substring - 02-03-2006 , 07:18 AM






thanks
"Randy Webb" <HikksNotAtHome (AT) aol (DOT) com> escribió en el mensaje
news:W8udnTyGtY8W1H7eRVn-gg (AT) comcast (DOT) com...
Quote:
Daviso said the following on 2/3/2006 6:36 AM:
Hi.

I hava an input like this
td><input name=numberA> </td

and a button with a javascript function in onclick event.

I want to control that the first element of the value I have introduced
in numberA will be different and make a conditional

for example
43543, I want to control the first digit (4)
5433, I watn to control the first digit(5)

I'm using "substring" but it seems doesn't work
I have:
If (document.F.numberA.value.substring(1,1) == "4")
or
If (document.F.numberA.subtring(1,1) == "4")

but no way. How can I do this????

if != If. JS is case-sensitive.

But, if all you want is the first character, charAt might be a better
solution:

if (document.F.numberA.value.charAt(0) == "4")

if (document.F.numberA.value.charAt(0) == "5")

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/



Reply With Quote
  #3  
Old   
Randy Webb
 
Posts: n/a

Default Re: Substring - 02-03-2006 , 07:22 AM



Daviso said the following on 2/3/2006 6:36 AM:
Quote:
Hi.

I hava an input like this
td><input name=numberA> </td

and a button with a javascript function in onclick event.

I want to control that the first element of the value I have introduced in
numberA will be different and make a conditional

for example
43543, I want to control the first digit (4)
5433, I watn to control the first digit(5)

I'm using "substring" but it seems doesn't work
I have:
If (document.F.numberA.value.substring(1,1) == "4")
or
If (document.F.numberA.subtring(1,1) == "4")

but no way. How can I do this????
if != If. JS is case-sensitive.

But, if all you want is the first character, charAt might be a better
solution:

if (document.F.numberA.value.charAt(0) == "4")

if (document.F.numberA.value.charAt(0) == "5")

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #4  
Old   
mick white
 
Posts: n/a

Default Re: Substring - 02-03-2006 , 09:56 AM



Daviso wrote:
[...]

Quote:
43543, I want to control the first digit (4)
5433, I watn to control the first digit(5)

I'm using "substring" but it seems doesn't work
I have:
If (document.F.numberA.value.substring(1,1) == "4")
or
If (document.F.numberA.subtring(1,1) == "4")
if(document.F.numberA.value.substring(0,1)=="4")

or follow Randy's advice.
Mick
[...]


Reply With Quote
  #5  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Substring - 02-03-2006 , 01:40 PM



Daviso wrote:

Quote:
I hava an input like this
td><input name=numberA> </td
While this particular attribute value needs not be quoted, all
attribute values better should be single-quoted or double-quoted.

<URL:http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2>

Quote:
and a button with a javascript function in onclick event.

I want to control that the first element of the value I have introduced in
numberA will be different and make a conditional

for example
43543, I want to control the first digit (4)
5433, I watn to control the first digit(5)

I'm using "substring" but it seems doesn't work
I have:
If (document.F.numberA.value.substring(1,1) == "4")
or
If (document.F.numberA.subtring(1,1) == "4")
I presume that resulted in

Quote:
SyntaxError: missing ; before statement
(because it is `if', not `If') and

Quote:
TypeError: document.F.numberA.value.substring is not a function
(because it is `substring', not `subtring'.)

<URL:http://jibbering.com/faq/#FAQ4_43>

Quote:
but no way. How can I do this????
If the button is part of the same form as the element named `numberA':

<form ...>
...
<script type="text/javascript">
function foo(f)
{
... f.elements['numberA'].charAt(0) ...
}

document.write(
'<input type="button" ... onclick="foo(this.form);">');
</script>
...
<input name="numberA">
...
</form>

If it is part of another form:

... document.forms['F'].elements['numberA'].charAt(0) ...


PointedEars


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.