2012-12-06 2 views
0

필자가 작성한 기능은 입력이 글자인지 아닌지 확인합니다. 그렇지 않으면 경고 메시지를 표시 한 다음 내용을 지 웁니다. 방금 편지를 입력하면 잘 작동하지만 숫자를 입력하면 글자를 입력하는 메시지가 표시됩니다.하지만 상자에 글자를 입력하면 메시지가 계속 표시되어 편지를 입력하라고 요청합니다. 왜? 어떻게 해결할 수 있습니까?입력 텍스트의 문자 만 검색합니다 (자바 스크립트를 사용하는 오류)

<%//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/> 

답변

0

문자를 변경하여 /^\s*[A-Za-z]+\s*$/과 일치 시키거나 값 주변의 공백을 제거하십시오.

+0

현재 작동하고 있습니다. 그게 무슨 뜻입니까? – keivn

+0

값 시작과 끝의 공백을 무시합니다. – redolent

+1

'^'는 문자열의 시작과 일치하고, \ s는 공백 문자를 의미하며,'* '는 0 이상을 의미하므로'\ s *'는 0 개 이상의 공백 문자입니다. '[A-Za-z]'는 물론 A-Z 또는 a-z 범위의 문자이고'+'는 하나 이상이며'$'는 문자열의 끝입니다. https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions –

2

값을 문자가 아닌 공백으로 설정합니다.

이름에 공백이있는 사람도 있습니다. 숫자에 대해서도 마찬가지입니다.

+0

하지만이 값 뒤에 공백으로 값을 설정해야합니다. 값이 문자가 아닙니다. – keivn

+1

@keivn, 내가해야 할 이유가 없습니다. –

+0

왜냐하면 사용자가 제출하기 전에 번호를 입력하지 못하게해야하기 때문입니다. – keivn

관련 문제