2015-01-27 4 views
0

편집 : 내 질문에 잘못되었습니다. 필요한 DOB 필드를 비워두면 사용자 DOB가 17 세 이상인 경우에만 말하고 싶은 'Welcome'이라고 표시됩니다. 환영 메시지가 나온 후 필요한 DOB 필드를 채우라고 알려줍니다. DOB가 입력되지 않은데도 여전히 환영받는 이유는 무엇입니까?자바 스크립트 - 필수 입력란이 비어있는 양식 제출

출산 필드의 날짜 생년월일 : 출생 확인 기능의

<input type="text" name="Date" id="dateof" placeholder="YYYY-MM-DD" title="Enter your date of birth in the form YYYY-MM-DD" required /><br/><br/> 

날짜

var minAge = 17; 
var today = new Date() 

function calcAge(birthDate, todaysDate) { 
    todaysDate = new Date(today); 
    var givenDOB = document.getElementById('dateof').value; /*date of birth entered*/ 
    var birthDate = new Date(givenDOB); 
    var years = (todaysDate.getFullYear() - birthDate.getFullYear()); 

    if (todaysDate.getMonth() < birthDate.getMonth() || 
     todaysDate.getMonth() == birthDate.getMonth() && todaysDate.getDate() < birthDate.getDate()) { 

     years--; 
    } 

    return years; 
} 


function setAge() { 

    var age = calcAge(); 
    if (age < minAge) { 
     alert("Sorry, the minimum age is 17!"); 
    } else 

     alert("Welcome!"); 

} 
+0

아마도 브라우저가 필요하거나 필수 입력을 시도하십시오. –

+0

모든 양식 코드를 공유 할 수 있습니까? –

답변

1

당신은 사용할 수

여기에 내가 생각하는 모든 코드는 관련이 있어요 javascript는 빈 날짜 필드를 감지하고 al을 보냅니다. ert.

자바 스크립트를 수정해야합니다. 다음과 같이 시도하십시오 :

관련 문제