JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

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 Dev Shed Forums Sponsor:
  #1  
Old August 17th, 2004, 11:04 PM
jomargon jomargon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 6 jomargon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
use Javascript "Onkeypress" command to accept only "numeric"

Good day. Is there a way to filter typed inputs right upon typing the characters using Javascript? i plan on using Javascript's "Onkeypress" command to accept only "numeric, i.e., 1,2,3...", ". (dot)", and ", (comma)" command. please help.thanks in advance.

Reply With Quote
  #2  
Old August 18th, 2004, 12:37 AM
titania titania is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 8 titania User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post As for IE

If browser is only IE, like this.
Code:
function element_keypress() {
  // nonumber , It doesn't react.
  if(!(event.keyCode>=0x30 && event.keyCode<=0x39)) {
     event.returnValue = false;
  }
}


but the other browser, I don't know.

sorry.

Reply With Quote
  #3  
Old August 18th, 2004, 09:29 AM
Dusk's Avatar
Dusk Dusk is offline
Twilight Thinking
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2003
Location: Oxford UK
Posts: 656 Dusk User rank is Sergeant Major (2000 - 5000 Reputation Level)Dusk User rank is Sergeant Major (2000 - 5000 Reputation Level)Dusk User rank is Sergeant Major (2000 - 5000 Reputation Level)Dusk User rank is Sergeant Major (2000 - 5000 Reputation Level)Dusk User rank is Sergeant Major (2000 - 5000 Reputation Level)Dusk User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 4 h 40 m 3 sec
Reputation Power: 36
Use regular expressions, ie:

Code:
function doIt()
{
	entry = document.getElementById('number').value;
	pattern = /[^0-9.,]/g;
	if (entry.match(pattern))
	{
		alert("Invalid characters present!");
	}
	else
	{
		alert("All ok!");
	}
}


and the form:

Code:
<form>
<input type="text" name id="number" />
<input type="button" value="Test It" onclick="doIt();" />
</form>


Hope that helps
__________________
Cheers,
Dusk

My portfolio

Reply With Quote
  #4  
Old August 18th, 2004, 11:46 AM
mikepurvis mikepurvis is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Toronto
Posts: 51 mikepurvis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 33 sec
Reputation Power: 8
Send a message via MSN to mikepurvis
I asked this exact question not too long ago...

The solution that I arrived at was this:

PHP Code:
<html>
<
head>

<
script type="text/javascript">
<!--
var 
validNums '0123456789.';
var 
validInt '0123456789';
var 
validLetters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

function 
validateKeyPress(evalidSet)
{
    var 
key;
    var 
keychar;
        
    if(
window.event || !e.which// IE
        
key e.keyCode// IE
    
else if(e
        
key e.which;   // Netscape
    
else
        return 
true;     // no validation

    
keychar String.fromCharCode(key);
    
validSet += String.fromCharCode(8);

    if (
validSet.indexOf(keychar) < 0)
      return 
false;

    return 
true
}
//-->
</script>

</head>
<body>
    <form name="myForm">
        <input type="text" size="50" onKeyPress="return validateKeyPress(event, validNums)" />
    </form>
</body>
</html> 


Keep in mind that this won't prevent invalid characters from being pasted into the box... you'd need an onChange or something to trap that. But I think the easier thing is to just use this and then validate server-side as well. (which should be done anyways, what with form-spoofing and all)

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > use Javascript "Onkeypress" command to accept only "numeric"


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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap