<!Doctype html> <html xmlns="http://www./1999/xhtml"> <head> <title>字符串实例</title> <style type="text/css"> #show{ width:200px; height:200px; margin:0 auto; color:white; background:#2c2c2c; text-align:center }
</style>
<script type="text/javascript"> //字符串实例: 邮箱验证 function check(){ var obj= document.getElementById("email") //得到元素 通过id 获得 //值 alert(obj.value) //值 if (obj.value!=""){ var str =obj.value if (str.indexOf("@")!=-1){ if (str.indexOf(".")!=-1){ var a =str.split("@") //拆分成数字 if(a[0].length>1){ document.getElementById ("mess").innerHTML="<font color='green'>正确</font>" }else{ document.getElementById ("mess").innerHTML="<font color='red'>@长度错误</font>" } }else{ document.getElementById ("mess").innerHTML="<font color='red'>没有.错误</font>" //没有
}
}else{ // document.getElementById ("mess").innerText="<font color='red'>错误</font>" //没有Value innerText内部文本 document.getElementById ("mess").innerHTML="<font color='red'>错误</font>" //没有 } } }
</script>
</head>
<body>
<label>email:</label><input type="text" id="email"/>
<input type ="button" value="验证" onclick ="check()"/>
<span id="mess"></span>
</body>
</html>
|