私が書いた関数は、入力が文字かどうかをチェックします。そうでない場合は、警告メッセージが表示され、コンテンツが消去されます。文字を入力するだけでは問題なく動作しますが、数字を入力すると、文字を入力するメッセージが表示されますが、ボックスに文字を入力すると、メッセージがポップアップして文字を入力するように求められます. なんで?どうすれば修正できますか?
<%//javascript file %>
<script type="text/javascript" >
function allLetter(value)
{
var letters = /^[A-Za-z]+$/;
if(value.match(letters))
{
return true;
}
else
{
alert("first, last and middle name contain only letters");
return false;
}
}
function checkform ( form )
{
// see http://www.thesitewizard.com/archive/validation.shtml
// for an explanation of this script and how to use it on your
// own website
// ** START **
if ((form.firstName.value == "") || (form.lastName.value == "") ) {
alert("Please enter both your first and last name.");
form.firstName.focus();
form.lastName.focus();
return false ;
}
// ** END **
return true ;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Names and country of residence page</title>
</head>
<body>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
// Registering Postgresql JDBC driver with the DriverManager
Class.forName("org.postgresql.Driver");
// Open a connection to the database using DriverManager
conn = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/assignment1",
"postgres","Km81920265");
// Create the statement
Statement statement = conn.createStatement();
// Use the created statement to SELECT
// the student attributes FROM the Student table.
rs = statement.executeQuery("SELECT * FROM countries_and_states WHERE is_country='t'");
%>
<%//first delcare input %>
Please enter your first name, last name and middle initial:<p>
<form method="get" action="address.jsp" onsubmit="return checkform(this);">
<%//store input into session %>
Your first name :<input type="text" size="15" name="firstName" onchange="if(!allLetter(this.value)){this.value= ' ';} "/><p/>
Your last name :<input type="text" size="15" name="lastName" onchange="if(!allLetter(this.value)){this.value= ' ';} "/><p/>
Your middle name:<input type="text" size="15" name="middleName" onchange="if(!allLetter(this.value)){this.value= ' ';} "/><p/>