首页 > 代码库 > JS(三)

JS(三)

1.检查用户名中是否含有特殊字符:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>检查输入数据中是否含有特殊字符</title>
</head>
<body>
<script type="text/javascript">
var str = window.prompt("敢问阁下高姓大名?");
var arr = ["*","%","&","<",">","/","\\","!","@","#"]
var length1 = str.length;
var length2 = arr.length;
var count = 0;
for(var i=0; i<length1; i++){
    for(var j=0; j<length2; j++){
        if(str[i]==arr[j]){
            count++;
            break;
        }
    }
    if (count!=0) {break;}
}
if (count==0) {document.write("用户名符合要求.");}
if (count!=0) {document.write("用户名含有特殊字符.");}
</script>    
</body>
</html>

2.大多数人的一生只有2w多天:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>活了多少天</title>
</head>
<body>
<script type="text/javascript">
var str = window.prompt("交出你的出生日期,如:1990-08-24");
var arr = str.split("-");
for(var i=0,length=arr.length; i<length; i++){
    arr[i] = Number(arr[i]);
}
var today = new Date().getTime();
var mybth = new Date(arr[0],arr[1]-1,arr[2]).getTime();
var days = parseInt((today-mybth)/1000/3600/24);
alert("少年,你已经在这个伟大的星球生活了"+days+"天!!!");
</script>    
</body>
</html>

 

JS(三)