2012-12-06 72 views
-2

我有問題,它沒有檢查我輸入的文本是隻有字母或不是?我真的不知道如何在jsp和Ajax中編寫JavaScript?有人可以通過發佈我可以學習的好鏈接來幫助嗎?特別是使用Ajax來電來訪數據庫jsp上的javascript不能正常工作?

是我的問題,現在,我還怎麼能檢查文字大小是大於0

<%@page import="java.util.*,support.*,java.sql.*"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 

<%//javascript file %> 
<script> 
function allLetter(value) 
{ 
var letters = /^[A-Za-z]+$/; 
if(inputtxt.value.match(alphaExp)) 
    { 
    return true; 
    } 
else 
    { 
    alert("message"); 
    return false; 
    } 
} 
</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"> 
<%//store input into session %> 
Your first name :<input type="text" size="15" name="firstName" onchange="allLetter(this.value); "/><p/> 
Your last name : <input type="text" size="15" name="lastName"/><p/> 
Your middle name:<input type="text" size="15" name="middleName"/><p/> 

<%//display dropdown menu using for looop %> 
Provide country information menu:<p> 
Country: 
<select name="Countryid"> 

<% 




while(rs.next()){ 
%> 
<option value="<%=rs.getInt("cs_id")%>"><%=rs.getString("country_state")%> 
</option> 
<%} %> 
</select> 

<p> 
<%-- -------- Close Connection Code -------- --%> 
      <% 
       // Close the ResultSet 
       if(rs != null){ 
        rs.close(); 
       } 

       // Close the Statement 
       statement.close(); 

       // Close the Connection 
       conn.close(); 


%> 

<%///closing up the entry page and submit the data %> 
<input type="submit" value="Submit Personal Data" onclick="this.disabled=true;"/> 

</form> 
</body> 
</html> 

回答

0

你傳入輸入元素allLetter。你應該寫這樣的方法。

function allLetter(inputtxt) 
{ 
var letters = /^[A-Za-z]+$/; 
if(inputtxt.value.match(alphaExp)) 
    { 
    return true; 
    } 
else 
    { 
    alert("message"); 
    return false; 
    } 
} 
+0

那麼如何函數知道哪個輸入文本?因爲我需要檢查所有的名字 – keivn

+0

因爲OP發送值不是對象'onchange =「allLetter(this.value);' –

+0

所以我怎麼能把它發送到函數? – keivn

0

試試這個,我改變了inputtxt.valuevalue

function allLetter(value) 
{ 
var letters = /^[A-Za-z]+$/; 
if(value.match(letters)) //removed inputtxt and added `letters` 
    { 
    return true; 
    } 
else 
    { 
    alert("message"); 
    return false; 
    } 
} ​ 

演示:http://jsfiddle.net/H44j6/

UPDATE

改變這一行

Your first name :<input type="text" size="15" name="firstName" onchange="allLetter(this.value); "/><p/> 

Your first name :<input type="text" size="15" name="firstName" onkeydown="allLetter(this.value); "/><p/> 

Your first name :<input type="text" size="15" name="firstName" onblur="allLetter(this.value); "/><p/> 
+0

沒有...它仍然沒有工作....所以我怎麼能通過輸入值的功能? – keivn

+0

你已經傳遞值你怎麼知道它不工作?你也在使用'onchange'在文本框中,嘗試使用'onblur'或'onkeydown'或'onkeypress' –

+0

更新我的上面的答案 –