Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onKeypress="noLet()" - - function noLet(){event.keycode} 2

Status
Not open for further replies.

SPYDERIX

Technical User
Jan 11, 2002
1,899
CA
Hi,

This is really stumping me? I have a form that calls a Javascript function and uses event keycode numbers to restrict people from inputting letters, therefore eliminating the need for form validation. However it is still allowing these characters (but it shouldn't):
!#$%&*()+-':",./
I want the users to only input numbers 0-9 and the decimal button.

Here is my code:

<HTML>
<HEAD>
<TITLE>CALCULATOR</TITLE>
<SCRIPT LANGUAGE=&quot;Javascript&quot;>
function noLet()
{
if (event.keycode < 47 || event.keyCode > 58) event.returnValue = false;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE=&quot;NUMBER&quot; SIZE=&quot;10&quot; NAME=&quot;a&quot; onKeypress=&quot;noLet()&quot; class=&quot;colors2&quot;>
</FORM>
</BODY>
</HTML>

Can someone help me, so that only numbers 0-9 and decimal are used and nothing else.

Thanks!
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
// let them put it
// and then remove all non 0-9 number
var unfiltred = document.formname.a.value;
var is_length = unfiltred.length;
var look = 0;
var filtred = &quot;&quot;;
while (look < is_length) {
if (unfiltred.charAt(look) == &quot;0&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;1&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;2&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;3&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;4&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;5&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;6&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;7&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;8&quot;) {
filtred += unfiltred.charAt(look);
} else {
if (unfiltred.charAt(look) == &quot;9&quot;) {
filtred += unfiltred.charAt(look);
}
}
}
}
}
}
}
}
}
}
look++;
}

// now filtred contain only the 0-9 value of unfiltred
// hope this helps someone knowledge ends where
someone else knowledge starts
 
This will work, I tried it out myself.

<SCRIPT LANGUAGE=&quot;Javascript&quot;>
function noLet()
{
if (event.keyCode == 46 || event.keyCode == 48 || event.keyCode == 49 || event.keyCode == 50 || event.keyCode == 51 || event.keyCode == 52 || event.keyCode == 53 || event.keyCode == 54 || event.keyCode == 55 || event.keyCode == 56 || event.keyCode == 57) event.returnValue = true;
else event.returnValue = false;
}
</SCRIPT>

GL &quot;Nothing is Impossible!&quot;
 
NICE!!!

Thanks swat
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
OK, final problem.

How can I prevent someone from pushing the decimal button twice in a form. These are math calculations, so they can't have 2 decimals, and someone is bound to push the decimal key more than once, by accident.

Is that even possible?

Thanks!
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
var trigger = 0;
...
function noLet()
{
if (event.keyCode == dot?) {
if (trigger == 0) {
trigger = 1;
} else {
if (event.keyCode == 46 || event.keyCode == 48 || event.keyCode == 49 || event.keyCode == 50 || event.keyCode == 51 || event.keyCode == 52 || event.keyCode == 53 || event.keyCode == 54 || event.keyCode == 55 || event.keyCode == 56 || event.keyCode == 57) {
event.returnValue = true;
} else {
event.returnValue = false;
}
}
}
someone knowledge ends where
someone else knowledge starts
 
NEVERSLEEP,

That didn't work. It says there is an error on the page, and it displays letters now too.

Thanks!
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
did u replace dot?[/red] by it proper numerical value ? someone knowledge ends where
someone else knowledge starts
 
yes I did and I forgot to add var trigger = 0; at first, but then added it and I still get the problem.

Thanks!
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
witch number is . event ? someone knowledge ends where
someone else knowledge starts
 
BTW the dot is # 46.
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
var trigger = 0;
// on each &quot;onFocus event&quot; on a new input u must use resetrigger()
function resetrigger() {
trigger = 0;
}
function noLet()
{
if (event.keyCode == 48 || event.keyCode == 49 || event.keyCode == 50 || event.keyCode == 51 || event.keyCode == 52 || event.keyCode == 53 || event.keyCode == 54 || event.keyCode == 55 || event.keyCode == 56 || event.keyCode == 57) {
event.returnValue = true;
} else {
if (event.keyCode == 46) {
if (trigger == 0) {
trigger++;
event.returnValue = true;
} else {
event.returnValue = false;
}
} else {
event.returnValue = false;
}
}
} someone knowledge ends where
someone else knowledge starts
 
OK, it works SORT OF...

If the user for some strange reason clicks on the form, then presses decimal, then goes to another one, then goes back to the first one and pushed decimal again, it will display another decimal (but only one).

Have I done something wrong? I put onFocus=&quot;resetrigger()&quot; onKeypress=&quot;noLet()&quot; in each input form, or is this this best it is going to work?

But what you've done is really good, thanks!
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
OH NO,

This is a problem. Now if the user puts in 20.5 but meant to put 2.5 then when they erase the whole thing and try to put in a new decimal, the form won't allow that.

Any suggestions!
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
What about dis-abling the backspace button. Is that possible? And then I will just write a note at the bottom telling users to press CLEAR is they screw up, and not let them hit backspace.

Is it possible yes/no???
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
u could do something like
//... declare the var before...
// b eval...
loopper = 0;
checker = 0;
while (looper < a.lenght) {
charcheck = a.charAt;
if (charcheck == '.') {
checker++;
}
loopper++;
}
if (checker > 0) {
alert(&quot;do not use twice the dot in a input&quot;);
} else {
// calcul here
}

hope this help someone knowledge ends where
someone else knowledge starts
 
while (looper < a.lenght) {
charcheck = a.charAt;
if (charcheck == '.') {

should be ...

while (looper < a.lenght) {
charcheck = a.charAt(looper);
if (charcheck == '.') {

.. oups someone knowledge ends where
someone else knowledge starts
 
ok where do I put that part of the script. I'm afraid I dont' know much about Javascript and what I had created was just copy and pasted from variuos sources on the net. Your code that looks likt this should go where?

loopper = 0;
checker = 0;
while (looper < a.lenght) {
charcheck = a.charAt(looper);
if (charcheck == '.') {
checker++;
}
loopper++;
}
if (checker > 0) {
alert(&quot;do not use twice the dot in a input&quot;);
} else {
// calcul here
}

Thanks again!
colorado.gif

&quot;Quest for the Cup - 2002!&quot;
&quot;Onto the 2[sup]nd[/sup] round!&quot;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top