KB Article 10431
2014/04/09

Close
X
Contact Tech Support

 Email Tech Support
 (250) 655-1766
 [7:30 - 5:00 PST]
Your Meeting ID will be Provided by a Rep.
Tech Support
Tech Support Home
Software Updates
Knowledge Base Search
Recent KB Articles
Product FAQs
Code Samples
Documentation
Tech Blog
System Requirements
Installation Instructions
Support Testimonials
Contact us to...
Create a Ticket
Request a Free Demo
Suggest a New Feature
Submit Feedback
Upload a Large File
Other Services
Software Training
Professional Services

Checking Numeric Data Type with JavaScript

Product: WebSmart ILE Type: Frequently Asked Question

HTML inherently only submits data in either string or binary. Here is the official spec: http://www.w3.org/TR/html401/interact/forms.html#form-content-type

For that reason, when you use JavaScript, it always comes in as string first, then it can be converted/parsed into numeric data. However this process can get a little confusing because certain data can have other meanings. For example, -1 can be interpreted as false, or 5e2 which is the scientific notation for 5x10^2 can be recognised as string because of the "e". You can check out some other samples over at this StackOverflow's thread.

There is no 100% guaranteed way to validate this, and there will always be an edge case. However, there is an accepted standard way:

!isNaN(parseFloat(n)) && isFinite(n)

isNaN is a popular go to choice for many web developer and you can see how it fares against others at this test page.

A better solution to this is to use a 3rd party extension that is well developed and maintained. One is jQuery Validation. You can check out them out at http://jqueryvalidation.org/. We also have Sample 185 which shows how to add jQuery Validation to your WebSmart program.

Rate This Article

Did this example help you to achieve your goal?
 Yes  No  Don't Know

Enter additional comments below.   If you want to hear back from us, include your contact information.

Email Address:
Comments:
Please enable JavaScript in order to rate this page.