JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

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 July 7th, 2007, 02:45 PM
doctormelodious's Avatar
doctormelodious doctormelodious is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 122 doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 9 h 9 m 2 sec
Reputation Power: 64
Can't determine whether form field contains a number

Greetings,

I am trying to validate an HTML form field, to make sure it contains a number.

In Attempt #1, even when the field contains a number, JavaScript interprets it as a string:

Code:
/*ATTEMPT #1*/

function validateProdNum(prodNumField){
	if(typeof prodNumField.value != "number"){
		alert('Production Number must be a number');
		return false;
	}
	else{return true}
}


In Attempt #2, if the field doesn't contain a number, the "alert" says "NaN," but my attempt to test for that in the "if" statement doesn't work. JavaScript thinks it IS a number, and returns TRUE:

Code:
/*ATTEMPT #2*/

function validateProdNum(prodNumField){
	alert(prodNumField.value/1)
	if(prodNumField.value/1 == "NaN"){
		alert('Production Number must be a number');
		prodNumField.focus();
		prodNumField.select();
		return false;
	}
	else{return true}
}


How can I make this work??

Thanks!
DM

Reply With Quote
  #2  
Old July 7th, 2007, 03:47 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,834 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 21 h 3 m 17 sec
Reputation Power: 4192
The values of form controls are always strings. In JavaScript a string will be automatically converted to a number if you try to subtract, multiply, or divide it by a number. (Adding doesn't work because the addition operator plays double duty as the concatenation operator.)

You can use Number() to convert a string to a number and to check if the result is a number or not, you must use the isNaN() function.
Code:
if(isNaN(Number(prodNumField.value))){

Number() will convert an empty string or a string containing only whitespace characters to 0, so since you need to test for that possibility as well, you might as well just do the whole check with a regular expression.

Code:
function isValidNumber(str) {return (/^(\d[\d\.]+|\.?\d)$/.test(str));}

if(!isValidNumber(prodNumField.value)){
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #3  
Old July 7th, 2007, 04:34 PM
doctormelodious's Avatar
doctormelodious doctormelodious is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 122 doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 9 h 9 m 2 sec
Reputation Power: 64
Thanks Kravvitz!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Can't determine whether form field contains a number

Developer Shed Advertisers and Affiliates



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 - 2013, Jelsoft Enterprises Ltd.

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