HTML, JavaScript And CSS Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingHTML, JavaScript And CSS Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old June 26th, 2008, 11:05 AM
jpockets5 jpockets5 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 168 jpockets5 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 9 h 44 m 36 sec
Reputation Power: 3
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?
Comments on this post
ChiefWigs1982 disagrees: JavaScript is NOT the same Java.

Reply With Quote
  #2  
Old June 27th, 2008, 11:11 PM
markoc's Avatar
markoc markoc is offline
Contributing User
ASP Free Regular (2000 - 2499 posts)
 
Join Date: Nov 2003
Location: UK
Posts: 2,151 markoc User rank is First Lieutenant (10000 - 20000 Reputation Level)markoc User rank is First Lieutenant (10000 - 20000 Reputation Level)markoc User rank is First Lieutenant (10000 - 20000 Reputation Level)markoc User rank is First Lieutenant (10000 - 20000 Reputation Level)markoc User rank is First Lieutenant (10000 - 20000 Reputation Level)markoc User rank is First Lieutenant (10000 - 20000 Reputation Level)markoc User rank is First Lieutenant (10000 - 20000 Reputation Level)markoc User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 18 h 10 m 15 sec
Reputation Power: 201
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).

Reply With Quote
  #3  
Old June 28th, 2008, 06:27 AM
ChiefWigs1982's Avatar
ChiefWigs1982 ChiefWigs1982 is offline
Cunning Linguist
ASP Free Loyal (3000 - 3499 posts)
 
Join Date: Mar 2005
Location: I used to live at home, now I stay at the house
Posts: 3,385 ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)ChiefWigs1982 User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 50746 Folding Title: Beginner FolderFolding Points: 50746 Folding Title: Beginner FolderFolding Points: 50746 Folding Title: Beginner Folder
Time spent in forums: 1 Month 1 Week 3 Days 9 h 3 m 13 sec
Reputation Power: 307
Facebook
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!
Route of Queue | The General FAQ Thread | HOW TO POST A QUESTION

Sign up with Matched.co.uk and earn up to £15 per website every month!


Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingHTML, JavaScript And CSS Help > Help with onkeypress function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT