JavaScript data type validation method _javascript tips

Source: Internet
Author: User
Tags cdata object object

The

Recent validation of JavaScript data types has a new understanding that can be judged so simple and comprehensive.
We have customized methods such as Isstring,isnumber, IsDate, IsError, Isregexp, Isboolean, IsNull, isundefined, IsObject, etc. Now show yourself the JavaScript data type validation functions and test sets that you define:

<! DOCTYPE html>  

The following is a detailed description of the data types of JavaScript, divided into six types of data, the need for friends can come to the reference, I hope to help you
1, to determine whether the 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 determining whether a string type
The code is as follows:

<script type= "Text/javascript" > 
//<![ cdata[ 
document.write (isstring (' Test '), ' <br/> '); 
document.write (isstring), ' <br/> '; 
function isstring (str) {return 
(typeof str== ' string ') &&str.constructor==String; 
} 
]]> 
</script>

3 Determining whether a numeric type
The code is as follows:

<script type= "Text/javascript" > 
//<![ cdata[ 
document.write (isnumber (' Test '), ' <br/> '); 
document.write (Isnumber), ' <br/> '; 
function Isnumber (obj) {return 
(typeof obj== ' number ') &&obj.constructor==Number; 
} 
]]> 
</script>

4 Determine whether the date type
The code is as follows:

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

5 judge whether it is a function
The code is as follows:

<script type= "Text/javascript" > 
//<![ cdata[ 
document.write (isfunction (function test () {}), ' <br/> '; 
document.write (isfunction), ' <br/> '; 
function Isfunction (obj) {return 
(typeof obj== ' function ') &&obj.constructor==Function; 
} 
]]> 
</script>

6 Determine whether or not an object
The code is as follows:

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

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

Related Article

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.