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