2017-06-30 57 views
0

我得到了這樣那樣的問題 - 我需要在我的JSF/PrimeFaces頁,以檢查p:calendar值(用鍵盤用戶輸入)是空的,如果這麼清楚的錯誤消息如何檢查JS如果p:日曆值爲空?

這是我的JSF日曆片段

<div class="item"> 
    <p:outputLabel id="l_dateBirth" for="dateBirth" value="#{msgs['customerForm.dateBirth']}"/> 
    <p:calendar id="dateBirth" widgetVar="dateBirthVar" value="#{customerBean.customer.dateOfBirth}" 
       placeholder="MM/dd/yyyy" 
       showOn="button" navigator="true" 
       pattern="MM/dd/yyyy" yearRange="-120:+0" 
       mindate="#{dateBean.minDate}" 
       maxdate="#{dateBean.maxDateOfBirth}" 
       required="true" 
       requiredMessage="#{msgs['Error.dateBirth.mandatory']}" 
       disabled="#{customerBean.mode eq 'EDIT'}"> 
       <f:ajax execute="@this" event="dateSelect" render="m_dateBirth" /> 
       <p:ajax event="keyup" oncomplete="hideCustomerCalendarErrMsg('dateBirth')" /> 
       <f:validator binding="#{dateOfBirthValidator}"/> 
       </p:calendar><p:message id="m_dateBirth" for="dateBirth" display="text"/> 
</div> 

這是我的JS代碼片段

function hideCustomerCalendarErrMsg(variable) { 
    var inputField, msg; 
    inputField = PF('dateBirthVar').getDate(); 
    msg = document.getElementById("createEditCustomerForm:accordion:m_" +variable); 
    if (inputField.value === null) { 
     msg.innerHTML = ""; 
    } 
} 

的話,請幫我 - 它不工作。如果用戶清除日曆字段,我需要清除驗證器錯誤消息

回答

2

而不是調用getDate(),嘗試檢查輸入本身的空值。

function hideCustomerCalendarErrMsg(variable) { 
    var inputField, msg; 
    inputField = PF('dateBirthVar').input; 
    msg = document.getElementById("createEditCustomerForm:accordion:m_" + variable); 
    if (inputField.val() === "") { 
     msg.innerHTML = ""; 
    } 
} 

或者你仍然可以有你的代碼,因爲它是,但不是

if (inputField.value === null) 

轉到了

if (inputField === null) 
+0

我做到了(但也不要工作 - 函數hideCustomerCalendarErrMsg(variable){var inputField,msg; inputField = PF('dateBirthVar').input; msg = document.getElementById(「createEditCustomerForm:accordion:m_dateBirth」); (inputField.value === null){if(inputField.value === null){ msg.innerHTML =「」;}} –

+0

是的,因爲你仍然有'if(inputField.value === null)'用'if(inputField.val )===「」)'@DmitriyLaletin –

+0

我照你所說的去做 - function hideCustomerCalendarErrMsg(variable){var inputField,msg; inputField = PF('dateBirthVar')。input; msg = document.getElementById(「createEditCustomerForm:accordion:m_dateBirth」);如果(inputField.val ===「」){ msg.innerHTML =「」;}} –