Dmitriy Laletin
Dmitriy Laletin

Reputation: 5

How to check in JS if p:calendar value is empty?

I got such kind of problem - I need to check in my JSF/PrimeFaces page if p:calendar value (entered by user with keyboard) is empty and if it so to clear error message

this is my JSF Calendar snippet

 <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>

this is my JS snippet

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

so, please help me - it doesnt work. And I need to clear validator err message on the fly if user clear the field of calendar

Upvotes: 0

Views: 1084

Answers (1)

Hatem Alimam
Hatem Alimam

Reputation: 10048

Instead of calling getDate(), try to check the input itself for empty value.

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

Or you can still have your code as it is, but instead of

if (inputField.value === null)

Go for

if (inputField === null)

Upvotes: 2

Related Questions