By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Substring

P: n/a
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
Feb 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/
Feb 3 '06 #2

P: n/a
thanks
"Randy Webb" <Hi************@aol.com> escribió en el mensaje
news:W8********************@comcast.com...
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/

Feb 3 '06 #3

P: n/a
Daviso wrote:
[...]
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
[...]
Feb 3 '06 #4

P: n/a
Daviso wrote:
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>
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

| SyntaxError: missing ; before statement

(because it is `if', not `If') and

| TypeError: document.F.numberA.value.substring is not a function

(because it is `substring', not `subtring'.)

<URL:http://jibbering.com/faq/#FAQ4_43>
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
Feb 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.