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

How to check a form field?

P: n/a
I am a newbie with Javascript and would like to check if a text
provided by a users , during filling in an input field , has digits
only.
Any idea?
Thank you
L

Sep 15 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
PythonistL wrote:
I am a newbie with Javascript and would like to check if a text
provided by a users , during filling in an input field , has digits
only.
Any idea?
Thank you
L
Something like:

if (isNAN(document.getElementById('myTextField').valu e))
{
code if not number;
}
else
{
code if is number;
}
Sep 15 '06 #2

P: n/a
Jeff Paffett wrote on 15 sep 2006 in comp.lang.javascript:
PythonistL wrote:
>I am a newbie with Javascript and would like to check if a text
provided by a users , during filling in an input field , has digits
only.
Any idea?
Thank you
L
Something like:

if (isNAN(document.getElementById('myTextField').valu e))
{
code if not number;
}
else
{
code if is number;
}
"Not a number"
is not the same as
"String has digits only"

Try:

if (/[^\d]/.test(document.getElementById('myTextField').value )) {
code if not number;
} else {
code if is number;
}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 15 '06 #3

P: n/a

Evertjan. wrote:
Jeff Paffett wrote on 15 sep 2006 in comp.lang.javascript:
PythonistL wrote:
I am a newbie with Javascript and would like to check if a text
provided by a users , during filling in an input field , has digits
only.
Any idea?
Thank you
L
Something like:

if (isNAN(document.getElementById('myTextField').valu e))
{
code if not number;
}
else
{
code if is number;
}

"Not a number"
is not the same as
"String has digits only"

Try:

if (/[^\d]/.test(document.getElementById('myTextField').value )) {
code if not number;
} else {
code if is number;
}

If you wanted to prevent anything but digits being accepted while
typing, you could attach a function to the onkeypress event of the
textfield and return false for non-digit keystrokes. This way they
wouldn't show up when typed.

There's an example of the opposite on w3schools.com. I don't know how
clean the code is, but it looks like this:

<script type="text/javascript">
function noNumbers(e)
{
var keynum
var keychar
var numcheck

if(window.event) // IE
{
keynum = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
numcheck = /\d/
return numcheck.test(keychar)
}
</script>

<form>
Type some text (numbers not allowed):
<input type="text" onkeypress="return noNumbers(event)" />
</form>

Maybe some more seasoned developers could fix any bad coding.
>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 15 '06 #4

P: n/a
Tom Cole wrote on 15 sep 2006 in comp.lang.javascript:
If you wanted to prevent anything but digits being accepted while
typing, you could attach a function to the onkeypress event of the
textfield and return false for non-digit keystrokes. This way they
wouldn't show up when typed.
I think that it is a bad idea, to have the field not reacting to certain
key presses. It distubs and alienates the user.

[And it was not the OQ]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 15 '06 #5

P: n/a
Evertjan. wrote:
Tom Cole wrote on 15 sep 2006 in comp.lang.javascript:
>If you wanted to prevent anything but digits being accepted while
typing, you could attach a function to the onkeypress event of the
textfield and return false for non-digit keystrokes. This way they
wouldn't show up when typed.

I think that it is a bad idea, to have the field not reacting to certain
key presses. It distubs and alienates the user.

I would have to agree with you that having the browser
do flat out nothing as a response to a keystroke is very disturbing.

It probably would not be so bad , if *while* refusing
the keystroke , that the user received some feedback
like perhaps a <divappearing with a "numbers only please" hint
that went away when the user pressed a number.
Sep 15 '06 #6

P: n/a
JRS: In article <Xn********************@194.109.133.242>, dated Fri, 15
Sep 2006 13:33:47 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.netposted :
>"Not a number"
is not the same as
"String has digits only"
Pereant, inquit, qui ante nos nostra dixerunt.

>Try:

if (/[^\d]/.test(document.getElementById('myTextField').value )) {
code if not number;
} else {
code if is number;
}
\D == [^\d]

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Sep 15 '06 #7

P: n/a

Evertjan. wrote:
Tom Cole wrote on 15 sep 2006 in comp.lang.javascript:
If you wanted to prevent anything but digits being accepted while
typing, you could attach a function to the onkeypress event of the
textfield and return false for non-digit keystrokes. This way they
wouldn't show up when typed.

I think that it is a bad idea, to have the field not reacting to certain
key presses. It distubs and alienates the user.
It is also a very unreliable way to attempt to validate the field
content. - it is much better to look at the actual value of the field
as you suggested :-)
--
Rob

Sep 16 '06 #8

P: n/a
Dr John Stockton wrote on 16 sep 2006 in comp.lang.javascript:
JRS: In article <Xn********************@194.109.133.242>, dated Fri, 15
Sep 2006 13:33:47 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.netposted :
>>"Not a number"
is not the same as
"String has digits only"

Pereant, inquit, qui ante nos nostra dixerunt.
Dixit Aelius Donatus,
qui distingueri Tiberii Claudii Donati necesse est.
>>Try:

if (/[^\d]/.test(document.getElementById('myTextField').value )) {
code if not number;
} else {
code if is number;
}

\D == [^\d]
Certe!

\D === [^\d]


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 16 '06 #9

P: n/a
RobG wrote on 16 sep 2006 in comp.lang.javascript:
>
Evertjan. wrote:
>Tom Cole wrote on 15 sep 2006 in comp.lang.javascript:
If you wanted to prevent anything but digits being accepted while
typing, you could attach a function to the onkeypress event of the
textfield and return false for non-digit keystrokes. This way they
wouldn't show up when typed.

I think that it is a bad idea, to have the field not reacting to certain
key presses. It distubs and alienates the user.

It is also a very unreliable way to attempt to validate the field
content. - it is much better to look at the actual value of the field
as you suggested :-)
... which should be done, or at least repeated serverside.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 16 '06 #10

P: n/a
Evertjan. wrote:
RobG wrote on 16 sep 2006 in comp.lang.javascript:
>Evertjan. wrote:
>>Tom Cole wrote on 15 sep 2006 in comp.lang.javascript:

If you wanted to prevent anything but digits being accepted while
typing, you could attach a function to the onkeypress event of the
textfield and return false for non-digit keystrokes. This way they
wouldn't show up when typed.

I think that it is a bad idea, to have the field not reacting to certain
key presses. It distubs and alienates the user.
It is also a very unreliable way to attempt to validate the field
content. - it is much better to look at the actual value of the field
as you suggested :-)

.. which should be done, or at least repeated serverside.
Actually checking from both the client side *and* the
server side is my preference. I know that I myself have
often enough groaned just as I hit a [ submit ] button,
realizing I would have to wait for another whole page load
just to fix a minor typo seen just a click too late.

Sep 16 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.