Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-26-2004, 07:23 AM   PM User | #1
zobernjik
New to the CF scene

 
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
zobernjik is an unknown quantity at this point
Question validate fields for numeric values only

Hello guys,

How can I validate that users have entered text or anything else for that matter and give them a pop up message informing they can only enter numbers.

I am new to this as you can see by my question,I hope somebody can help.

Regards
zobernjik is offline   Reply With Quote
Old 04-26-2004, 07:46 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here is an example:-

<tr>
<td><font face="Arial"><em><b>Club Membership No. (if applicable)</b></em></font></td>
<td><input type="text" size="8" maxlength="8" name="MemNumber" onblur="if(/\D/g.test(this.value)){alert('Only current Club membership numbers are valid in this box. ');this.value='';this.focus()}; </font></td></tr>
Philip M is online now   Reply With Quote
Old 04-26-2004, 08:02 AM   PM User | #3
Kylena
New Coder

 
Join Date: Aug 2003
Location: Singapore
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Kylena is an unknown quantity at this point
Here's a script that works the same as Philip's.

Code:
var f = document.forms[0]

var v = f.NumField.value
for (i=0; i<v.length; i++)
{
	if ((v.charCodeAt(i)<48 || v.charCodeAt(i)>57) && v.charCodeAt(i) !== 44 ) 
	{
	alert ( "This must be numeric." )
	f.NumField.value = "";
	f.NumField.focus()
	return false
	}
}
Kylena is offline   Reply With Quote
Old 04-26-2004, 10:33 AM   PM User | #4
Garadon
Regular Coder

 
Join Date: Jul 2002
Posts: 698
Thanks: 0
Thanked 0 Times in 0 Posts
Garadon is an unknown quantity at this point
if(parseFloat(Field.value,10)==Field.value){}
or
if(parseInt(Field.value,10)==Field.value){}
Garadon is offline   Reply With Quote
Old 04-26-2004, 10:40 AM   PM User | #5
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
parseFloat doesn't need that second argument of 10 since it may only take decimal numbers, but the parseInt needs it.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 04-26-2004 at 10:43 AM..
liorean is offline   Reply With Quote
Old 04-26-2004, 10:43 AM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
parseFloat() has no radix parameter unlike parseInt(). It always parse numbers in base 10
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-26-2004, 10:43 AM   PM User | #7
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Quote:
Originally Posted by glenngv
parseFloat() has no radix parameter unlike parseInt(). It always parse numbers in base 10
Beat you to it
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 04-26-2004, 11:00 AM   PM User | #8
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:26 AM.


Advertisement
Log in to turn off these ads.