|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with onkeypress function
I have a form that the user can enter info, but i only want it to be numbers. So i have this Java function but i'm getting an error that says object required, i'm not sure since i'm really new to java.
This is the java... Code:
function nmbTst(){
if(event.keyCode == 13||(event.keyCode >= 48 && event.keyCode <= 57)){
event.returnValue = true ;
}
else {
alert("you can only enter numbers");
event.returnValue = false;
}
}
This is the html Code:
<td class="fieldHeading">
Retention <input type="text" name="RETENTION" value="$0.00" onkeypress="nmbTst()"></td></tr>
am I missing something? |
|
#2
|
||||
|
||||
|
Help with onkeypress function
you could try something like:
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
<script language="javascript">
<!--//
document.onkeydown = keyListener;
function keyListener(e){
if(!e){
e = window.event;
}
if (document.activeElement.id == "RETENTION")
{
if ((e.keyCode==13 || (e.keyCode >= 48 && e.keyCode <= 57)) == false) {
alert("you can only enter numbers");
e.returnValue = false;
}
}
}
//-->
</script>
</head>
<body>
<table>
<tr>
<td class="fieldHeading">Retention <input type="text" name="RETENTION" id="RETENTION" value="" />
</td>
</tr>
</table>
</body>
</html>
here is another example where the validation is done at submit and places 2 decimal points in the value Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Check Price Format Page</title>
<script language="javascript">
<!--//
function LTrim(value) {
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}
function RTrim(value) {
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}
function validateprice(field, itemname) {
if (field.value.length > 0) {
var priceval = "";
field = LTrim(RTrim(field.value));
if (field.charAt(0)=="$" || field.charAt(0)=="£") {
var a = 1;
}
else {
var a = 0;
}
var valid = "0123456789.";
var ok = "yes";
var temp;
var len;
//var c = 0;
for (var i=a; i<field.length; i++) {
temp = "" + field.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
priceval += temp;
}
if (ok == "no") {
alert("Invalid entry! \nYou can only enter a price value.");
}
else {
priceval = priceval * 1000
priceval = Math.round(priceval /10) + ""
while (priceval.length < 3) {priceval = "0" + priceval}
len = priceval.length
priceval = priceval.substring(0,len-2) + "." + priceval.substring(len-2,len)
if (priceval.length > 10) {
alert("Invalid entry!\n\nYou have entered a value to large.\nThe max value can enter is 9999999.99\nyour entry value was " + priceval + ".");
}
else {
document.getElementById(itemname).value = priceval;
}
}
}
}
function validateform() {
return false;
}
//-->
</script>
</head>
<body>
<form name="frm1" action="" method="post" onsubmit="return validateform()">
<input type="text" name="amount" id="amount" maxlength="10" onBlur="javascript:validateprice(this,this.name);" /> :Cost<br />
<input type="text" name="amount2" id="amount2" maxlength="10" onBlur="javascript:validateprice(this,this.name);" /> :Price<br />
<input type="text" name="dat" maxlength="7" /> :Product<br />
<br />
<input type="submit" value="submit" />
</form>
</body>
</html>
__________________
Hope this advise helps. ![]() If so please show your appreciation by adding reputation points (click gauge image on top right of this post and score).
|
|
#3
|
||||
|
||||
|
Wow - talk about reinventing the wheel guys!
![]() I think you should both look into things like the parseFloat() function and regular expressions... You could do all of this in only a couple of lines of code. <edit> Oh, and Markoc - advice is spelled with a 'c'! ![]()
__________________
Support requests via PM will be ignored! |
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Help with onkeypress function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|