JavaScript를 통해 HTML 문서에서 Firstname 및 Surname 필드의 유효성을 검사하려고합니다. 입력란에 번호 나 이상한 문자가 없어야 함을 의미하는 유효한 이름/성을 사용자가 삽입했는지 여부를 확인해야합니다.JavaScript 텍스트 유효성 검사가 Regex에서 작동하지 않습니다.
나는 해결책을 찾기 위해 stackoverflow와 다른 유사한 웹 사이트를 검색해 왔지만, 불행히도 내가 찾던 정확한 해결책을 찾지 못했기 때문에이 웹 사이트의 누군가 도움을 줄 수 있기를 바라고 있습니다.
<form name="contact" method="post" action="contact.php">
<table width="200" border="0" align="center">
<tr>
<td><p class="pagetext">Name:</p></td>
<td><input type="text" name="fname" id="fname" size="30"/></td>
</tr>
<tr>
<td><p class="pagetext">Surname:</p></td>
<td><input type="text" name="sname" id="sname" size="30"/></td>
</tr>
<tr>
<td><p class="pagetext">Email:</p></td>
<td><input type="text" name="email" id="email" size="30"/></td>
</tr>
<tr>
<td><input class="btns" type="submit" onclick="validationMain();validationContact();validationErrors();" value="Submit"/></td>
<td><input class="btns" type="submit" onclick="validationReset()" value="Reset"/></td>
</tr>
</table>
</form>
이 (문제를 복잡하게하지 않도록 관련이없는 부분을 왼쪽) 자바 스크립트 코드입니다 :
이
내 HTML 코드입니다var errors = "";
var textOnly = /^[A-Za-z]+$/;
function validationMain(){
var firstname = document.getElementById('fname');
var surname = document.getElementById('sname');
var email = document.getElementById('email');
//emailRegEx expression from www.regular-expressions.info/email.html
var emailRegEx = /^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (firstname.value == "") {
errors += "First Name Cannot Be Left Empty\r\n";
} else {
if (firstname.value != textOnly.value){
errors += "First Name is Invalid\r\n";
}
}
if (surname.value == "") {
errors += "Surname Cannot Be Left Empty\r\n";
} else {
if (surname.value != textOnly.value){
errors += "Surname is Invalid\r\n";
}
}
if (email.value != ""){
if (email.value.search(emailRegEx) == -1){
errors += "Invalid Email Entered\r\n";
}
} else {
errors += "Email Cannot Be Left Empty\r\n";
}
}
가장 큰 문제가 무엇이든 그 텍스트 필드에 입력 한 이름 또는 성 (숫자 나 특수 문자가없는 올바른 문자 일지라도) 유효하지 않은 문자를 입력하라는 메시지가 표시되므로 if가 잘못되었다고 추측합니다. 조건.
, 당신의 시간을 사전에 감사 도움 및 관련 정보 제공 :
내가 충분한 정보를 준 희망하지 않을 경우 알려 주시기 바랍니다!
[firebug] (http://getfirebug.com/) 또는 [WebKit inspector] (https://developers.google.com/chrome-developer-tools/)와 같은 디버거를 사용하여 암호? – RichardTowers