<!DOCTYPE html>
<html>
<head>
<title>js9.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function isEmail(){
var emailValue = document.getElementsByName("email")[0].value;
if(emailValue.indexOf("@") == -1){
alert("请输入正确的email地址");
}else{
alert("OK!");
}
}
</script>
</head>
<body>
<form>
Email:<input type = "text" name = "email">
<input type = "button" value = "check" onclick = "isEmail();">
</form>
</body>
</html>
说明:getElementByName("name")[0] 是用来返回单一结果。其中name属性是可以重复的,0就是指重复中的第一个元素。
indexOf是用来返回你所给定的字符在字符串中的位置,如果不存在则返回-1