How to check a form field?

P

PythonistL

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
 
J

Jeff Paffett

PythonistL said:
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').value))
{
code if not number;
}
else
{
code if is number;
}
 
E

Evertjan.

Jeff Paffett wrote on 15 sep 2006 in comp.lang.javascript:
Something like:

if (isNAN(document.getElementById('myTextField').value))
{
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;
}
 
T

Tom Cole

Evertjan. said:
Jeff Paffett wrote on 15 sep 2006 in comp.lang.javascript:
Something like:

if (isNAN(document.getElementById('myTextField').value))
{
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.
 
E

Evertjan.

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]
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 15
Sep 2006 13:33:47 remote, seen in Evertjan.
"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]
 
R

RobG

Evertjan. said:
Tom Cole wrote on 15 sep 2006 in comp.lang.javascript:


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 :)
 
E

Evertjan.

Dr John Stockton wrote on 16 sep 2006 in comp.lang.javascript:
JRS: In article <[email protected]>, dated Fri, 15
Sep 2006 13:33:47 remote, seen in Evertjan.


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]
 
E

Evertjan.

RobG wrote on 16 sep 2006 in comp.lang.javascript:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,355
Latest member
SJLChristi

Latest Threads

Top