변경시 텍스트 입력이 정규식을 사용하지 않고 자바 스크립트 만 사용하여 문자열에 숫자가 있는지 확인하는 방법이 있습니까?
예를 들어 (좋은 접근 방식이 아닐 수 있지만 확실하지 않습니다) :
const passwordInput = document.querySelector("#password");
passwordInput.addEventListener("change", e => {
if (e.target.value contains a number) {
console.log("Must contain a number.")
}
})
당신은 당신이 사용할 수있는, 그것을 반복하고, 각 문자를 확인하실 수 있습니다 !isNaN(x)
A는 있는지 확인하는 x
숫자로 변환 할 수 있으며, x.trim() !== ''
공백을 필터링 (덕분에 @Lain 에 대한 지적 이 isNaN
반환 false
공백에 대한) :
function hasNumbers(string){
return Array.prototype.some.call(string, c => !isNaN(c) && c.trim())
}
const passwordInput = document.querySelector("#password");
passwordInput.addEventListener("change", e => {
if (hasNumbers(e.target.value)) {
console.log("Contains a number.")
} else {
console.log("Doesn't contain a number.")
}
})
<input id="password" >
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다