<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form>
Name:<input type="text" id="t1" /> <br> Gender: <input
type="radio" name="sex" value="male">male <input type="radio"
name="sex" value="female">female <br> <input
type="submit" value="submit" onclick="myFunction()">
</form>
<script type="text/javascript">
var regexp = /^[a-zA-Z]+$/;
function myFunction() {
var str = document.getElementById("t1").value;
if (str == "") {
alert("plz enter anything");
}
else if (str.value.match(regexp)) {
alert("Letter Validation: Successful.");
} else {
alert("not valid input");
}
}
</script>
</body>
</html>
我想做字符验证。当用户输入除字符外它应该显示无效输入。但它既不工作也不显示任何错误消息。我不知道为什么它不工作......