Available CategoriesAdobeMacromediaProgrammingSQLServer AdministrationNetworkingMicrosoft ProductsMac OSLinux systemsMobile devicesXMLCertificationMiscAvailable Tutorials.NET Framework Essentials.NET Programming securityC# In A Nutshell TutorialProgramming C.SharpMastering Visual Studio .NETASP.NETWeb Solutions based on ASP.NET and ADO.NETJava data objectsJava extreme programmingJava performance tuningJava development on pda's. Building applications for pocket pc and palm devicesJavaScript and DHTMLLearning UMLUMLLearning XMLCocoaProgramming CppPerl objects, references and modulesPerl tutorialPython tutorialPython. Text processingPocket pc network programmingPHP & MySQL. Building web database applicationsPHP & MySQL. Programming for beginnersPHP, MySQL and Apache in 24 hoursSoftware architecture in practiceSoftware engineering and computer gamesBuilding Solutions With the Microsoft .NET Compact FrameworkProgramming Microsoft Visual C# 2005ActionscriptMastering Delphi 7Ado.netPractical mod_perlPerl for bioinformaticsWeb ServicesPrinciples of Secure CodingC/C++ Secure ProgrammingASP.NET AJAXVisual C#Borland C++ Builder 6 Developer's Guide |
2.2 Testing a Number's Validity
2.2.1 ProblemYou want to be sure a value is a number before performing a math operation on it. 2.2.2 SolutionIf the value you're testing can come from any kind of source, the safest bet is to use the typeof operator on the value. Applying this operator to any numeric value evaluates to the string number. Therefore, using it in a conditional expression looks like this: if (typeof someVal = = "number") {
// OK, operate on the value numerically
}
But some JavaScript methods, such as parseInt( ) and parseFloat( ), return a special value, NaN ("not a number"), signifying that they were unable to derive the number you desired. Operations expecting numeric operands or arguments that encounter values evaluating to NaN also generally return NaN. To test for this condition, use the isNaN( ) method, which returns true if the value is not a number. For example: var myVal = parseInt(document.myForm.myAge.value);
if (isNaN(myVal)) {
alert("Please check the Age text box entry.");
} else {
// OK, operate on the value numerically
}
2.2.3 DiscussionDon't get the wrong impression about the isNaN( ) method from the second example just shown. It is not a suitable approach to validating numeric input to a text box. That's because the parseInt( ) or parseFloat( ) methods return the first numbers (if any) they encounter in the string value passed as an argument. If someone enters 32G into a text box intended for an age, the parseInt( ) method pulls off the 32 portion, but the full value of the text box is not valid for your database that expects a strictly numeric value for that field. See Recipe 8.2 for more robust ways of validating numeric text entries.
Look to the NaN value as a debugging aid. If some calculation is failing, use alert dialog boxes to show the values of the operands and components. Any value that reports itself to be NaN means that it has problems at its source that need fixing before your calculation can even get started. As a point of trivia, the NaN value is, believe it or not, a number data type, and is also a property of the static Number object. 2.2.4 See AlsoRecipe 8.2 for numeric data entry validation in a form.
|