JavaScript sử dụng isNaN() để kiểm tra dữ liệu có phải là số? How to use isNaN() check variable is a number in JavaScript?

isNaN() – là từ viết tắt của "is Not a Number", nếu biến khôn phải là số thì nó sẽ trả về True, ngược lại thì sẽ trả về False.

Source check valid number with isNaN():

<html>
<title>VNLIVES.NET</title>
<body>
<h1>isNaN() example</h1>

<script type="text/javascript">
 var num1 = 100;
 if(isNaN(num1)){
    document.write(num1 + " is not a number <br/>");
 }else{
    document.write(num1 + " is a number <br/>");
 }

 var str1 = "VNLIVES.NET"
 if(isNaN(str1)){
    document.write(str1 + " is not a number <br/>");
 }else{
    document.write(str1 + " is a number <br/>");
 }
</script>

</body>
</html>


Demo check valid number with isNaN():




No comments:

Post a Comment