Javascript data type verification method, javascript Data Type

Source: Internet
Author: User

Javascript data type verification method, javascript Data Type

Recently, we have gained a new understanding of the javascript data type verification. It turns out to be so simple and comprehensive.
We have customized methods such as isString, isNumber, isDate, isError, isRegExp, isBoolean, isNull, isUndefined, and isObject. Now we will display our own javascript data type verification functions and test sets:

<! DOCTYPE html> 

The following describes in detail how to judge the javascript data type, which is divided into six data types. If you need it, you can refer to it for help.
1. Determine whether it is an array type
The Code is as follows:

<STRONG><script type="text/javascript"> //<![CDATA[ var a=[0]; document.write(isArray(a),'<br/>'); function isArray(obj){ return (typeof obj=='object')&&obj.constructor==Array; } //]]> </script></STRONG>

2. Determine whether the string type is used.
The Code is as follows:

<script type="text/javascript"> //<![CDATA[ document.write(isString('test'),'<br/>'); document.write(isString(10),'<br/>'); function isString(str){ return (typeof str=='string')&&str.constructor==String; } //]]> </script>

3. Determine whether the data type is Numeric.
The Code is as follows:

<script type="text/javascript"> //<![CDATA[ document.write(isNumber('test'),'<br/>'); document.write(isNumber(10),'<br/>'); function isNumber(obj){ return (typeof obj=='number')&&obj.constructor==Number; } //]]> </script>

4. Determine if it is of the date type
The Code is as follows:

<script type="text/javascript"> //<![CDATA[ document.write(isDate(new Date()),'<br/>'); document.write(isDate(10),'<br/>'); function isDate(obj){ return (typeof obj=='object')&&obj.constructor==Date; } //]]> </script>

5. determine whether it is a function.
The Code is as follows:

<script type="text/javascript"> //<![CDATA[ document.write(isFunction(function test(){}),'<br/>'); document.write(isFunction(10),'<br/>'); function isFunction(obj){ return (typeof obj=='function')&&obj.constructor==Function; } //]]> </script>

6. Determine whether the object is used
The Code is as follows:

<script type="text/javascript">linenum//<![CDATA[ document.write(isObject(new Object()),'<br/>'); document.write(isObject(10),'<br/>'); function isObject(obj){ return (typeof obj=='object')&&obj.constructor==Object; } //]]> </script>

I hope this article will help you learn about javascript programming.

Articles you may be interested in:
  • Code for verifying the validity of data on the JAVASCRIPT client (regular)
  • Simple encapsulation of js data verification set, js email verification, js url verification, js length verification, and js digital verification
  • JavaScript data verification implementation code in asp.net
  • Javascript cascade drop-down menu and core code for AJAX data verification
  • How to use JavaScript and regular expressions for data verification
  • Verify type data in a/B/c format using the js Regular Expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.