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;

 

{
a=eval(form.a.value);
b=eval(form.b.value);
loopper = 0;
checker = 0;
while (looper < a.length) {
charcheck = a.charAt(looper);
if (charcheck == '.') {
checker++;
}
loopper++;
}
if (checker > 0) {
alert(&quot;do not use twice the dot in a input&quot;);
} else {
c=a*b
form.ans.value=c
}
} someone knowledge ends where
someone else knowledge starts
 
That didn't work. Same problem, user presses decimal once then erases then they can't re-enter it. Also whatever you wrote isn't allowing the function to work at all. It comes up with an error, when I press the equals button, and nothing displays in the 3rd form.

Thanks!
colorado.gif

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

 
tested...

var looper = 0;
var lock = 0;
var check1;
var check2;

function a_div_b(form) {
while (looper < form.a.value.length) {
check1 = form.a.value.charAt(looper);
check2 = form.b.value.charAt(looper);
if ((check1 == &quot;.&quot;) || (check2 == &quot;.&quot;)) {
lock++;
}
looper++;
}
if (lock < 2) {
a=eval(form.a.value);
b=eval(form.b.value);
c=a/b;
form.ans.value = c;
} else {
alert('r u trying to f*ck this up ?');
}
looper = 0;
lock = 0;
} someone knowledge ends where
someone else knowledge starts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top