주제 줄에 유사한 스레드가 많이 있다는 것을 알고 있으므로 문제를 명확하게 설명 할 것입니다. 빈 필드에 대한 테스트가 완벽하게 실행되고 다른 여러 포럼 게시물을 따른 후 숫자 또는 알파벳 입력에 대한 테스트를 구현하려고 시도했지만 함수가 단순히 작동하지 않는다는 것을 검증하는 간단한 양식이 있습니다.JavaScript - 숫자/알파벳 만 유효성 확인
function validateNotEmpty(input, id)
{
var errorDisplay = document.getElementById(id);
var notEmpty;
if (input == "") // check if input is empty
{
errorDisplay.innerHTML = "This is a requiered field";
notEmpty = false; // update return value
} // end if
else
{
errorDisplay.innerHTML = ""; // clear the error area
notEmpty = true; // update return value
} // end else
return notEmpty; // return whether the input is empty
}// end function validateNotEmpty
그리고 다음 내 두 (작동하지 않는 숫자/알파벳 테스터)입니다 : 여기
내 업무는 NotEmpty 기능의 조각입니다 이 위에function validateNotNumber(input, id)
{
var errorDisplay = document.getElementById(id);
var notNumber;
for (var i=0;i<input.value.length;i++){
temp=input.value.substring(i,i+1);
console.log(temp);
if (digits.indexOf(temp)==-1){
errorDisplay.innerHTML = "Sorry, this section can not have numbers";
notNumber = false;
}
else
{
errorDisplay.innerHTML = ""; // clear the error area
notEmpty = true; // update return value
} // end else
}
return notNumber;
}
function validateNotLetter(input, id)
{
var errorDisplay = document.getElementById(id);
var notLetter;
for (var i=0;i<input.value.length;i++){
temp=input.value.substring(i,i+1);
console.log(temp);
if (alphabets.indexOf(temp)==-1){
errorDisplay.innerHTML = "Sorry, this section can not have letter";
notLetter = false;
}
else
{
errorDisplay.innerHTML = ""; // clear the error area
notLetter = true; // update return value
} // end else
}
return notLetter;
}
이가 어떤 도움/포인터에 대한
<td width="135" valign="top">
<input size="15" type="text" name="Full_Name" value="" onblur = "validateNotNumber(this.value, 'nameError')"/>
<p> <span id = "nameError" class = "errorMessage"></span> </p>
</td>
감사합니다, STIL : 난 내 양식에서 자바 스크립트 함수를 호출하고 방법 JavaScript에 익숙하지 않아 유효성 검사를 둘러싼 내 머리를 감싸는 것은 꽤 복잡해졌습니다.
정규식을 배울 수있는 장소 - 장소는 정규식의
https://regexone.com/을 테스트합니다. 시간이 걸리고 게시물을 정리하고 간단한 코드 예제를 작성하면 도움이 될 것입니다. – Nix
validateNotNumber 함수의 문제는 루핑하는 동안 input.value.length를 사용하는 대신 input.length 만 사용합니다.이 줄에도 동일하게 적용됩니다. temp = input.value.substring (i, i + 1); 더하기 당신은 정의되지 않은 숫자를 사용하고 있습니다. –