Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:51:01+00:00 2026-06-05T00:51:01+00:00

I have these date and time fields, and I want to set a javascript

  • 0

I have these date and time fields, and I want to set a javascript validation for the time.

If the format is invalid, it should make the label visible, else it should be invisible.

This is the code I have so far.

  <td nowrap="nowrap" align="left">
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td align="right" nowrap="nowrap">
                        <span id="startDateLabel">Start date/time: </span>
                        <input id="startDateStr" name="startDateStr" size="8" onchange="if (!formatDate(this,'USA')) {this.value = '';}" />
                        <button id="startDateCalendarTrigger">...</button>
                        <input id="startDateTime" type="text" size="8" name="startTime" value="12:00 AM" onchange="validateHHMM(this.value);"/>
                        <label id="startTimeLabel" visible="false">Time must be entered in the format HH:MM AM/PM</label>
                        <BR>
                        <span id="endDateLabel">End date/time: </span>
                        <input id="endDateStr" name="endDateStr" size="8" onchange="if (!formatDate(this,'USA')) {this.value = '';}" />
                        <button id="endDateCalendarTrigger">...</button>
                        <input id="endDateTime" type="text" size="8" name="endTime" value="12:00 AM" onchange="validateHHMM2(this.value);"/>
                        <label id="endTimeLabel" visible="false">Time must be entered in the format HH:MM AM/PM</label>
                    </td>
                </tr>
            </table>
        </td>

The problem is the label shows up when loaded irrespective of what I set as visible. I tried visibility = “hidden” and it still shows up.

Here is the validation part:

    <script>
function validateHHMM(inputField) {
    var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(inputField.value);

    if (isValid) {
        document.getElementById("startTimeLabel").style.visibility = "hidden";
    }else {
        document.getElementById("startTimeLabel").style.visibility = "visible";
    }

    return isValid;
}
function validateHHMM2(inputField) {
    var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(inputField.value);

    if (isValid) {
        document.getElementById("endTimeLabel").style.visibility = "hidden";
    }else {
        document.getElementById("endTimeLabel").style.visibility = "visible";
    }

    return isValid;
}
 </script>

So, how should I go about this? Google shows up different validation methods but not how to hide/show labels

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:51:03+00:00Added an answer on June 5, 2026 at 12:51 am

    You are looking for display:

    document.getElementById("endTimeLabel").style.display = 'none';
    document.getElementById("endTimeLabel").style.display = 'block';
    

    Edit: You could also easily reuse your validation function.

    HTML:

    <span id="startDateLabel">Start date/time: </span>
    <input id="startDateStr" name="startDateStr" size="8" onchange="if (!formatDate(this,'USA')) {this.value = '';}" />
    <button id="startDateCalendarTrigger">...</button>
    <input id="startDateTime" type="text" size="8" name="startTime" value="12:00 AM" onchange="validateHHMM(this.value, 'startTimeLabel');"/>
    <label id="startTimeLabel" class="errorMsg">Time must be entered in the format HH:MM AM/PM</label><br />
    
    <span id="endDateLabel">End date/time: </span>
    <input id="endDateStr" name="endDateStr" size="8" onchange="if (!formatDate(this,'USA')) {this.value = '';}" />
    <button id="endDateCalendarTrigger">...</button>
    
    <input id="endDateTime" type="text" size="8" name="endTime" value="12:00 AM" onchange="validateHHMM(this.value, 'endTimeLabel');"/>
    <label id="endTimeLabel" class="errorMsg">Time must be entered in the format HH:MM AM/PM</label>
    

    Javascript:

    function validateHHMM(value, message) {
        var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(value);
    
        if (isValid) {
            document.getElementById(message).style.display = "none";
        }else {
            document.getElementById(message).style.display= "inline";
        }
    
        return isValid;
    }
    

    Live DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table where there is a 'date_added' (date) field, 'time_added' (time)
T-SQL DateTime Question. I have a set of time ranges. During those time ranges
I have table consisting of these fields: id | date_from | date_to | price
I have table consisting of these fields: id | date_from | date_to | price
I have a VF page which has 2 date fields and search button. On
I have a DateTime variable: DateTime date = DateTime.Now; I want to change the
I'm using Entity Framework 4.1 Code First. In my entity, I have three date/time
I have a list of custom objects. These objects have 2 datetime properties on
I have this table: ID(INT) DATE(DATETIME) Under the DATE column there are a lot
I have downloaded the UI date picker with the theme start theme. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.