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 3938606
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:12:13+00:00 2026-05-20T00:12:13+00:00

I have a text box which accepts time(max of 5 characters only), and a

  • 0

I have a text box which accepts time(max of 5 characters only), and a drop down which accepts am or pm value.

I need to perform some validations for the string values entered into the text box such as:

  1. If user enters 9 => Should be changed to 0900
  2. 9:3 => 0930
  3. 09:3 => 0930
  4. 93 => alert (‘Invalid Hours value)
  5. 115 => 0115
  6. 12 => 1200
  7. Invalid entries such as !@#$%^&*()<>?/~`,;'”[]_-abcdefg.. => alert (‘Invalid Time Value’) should be displayed.

So far, all I’ve achieved is replacing the : with ”.
For example, if user enters 09:00 => 0900

I need something like:

if clks is 1 digit, then rightpad clks with 2 zeroes.

if 2 digits: clks > 12 , then alert(Invalid hours value)

if 3 digits: clks < (%59) (i.e checking last 2 chars) , then leftpad with 1 zero
or clks > (%59) , then alert (‘Invalid minutes value)

if 4 digits: clks>12% (checking first 2 chars), alert (‘invalid hours value’)
or (clks>12% and clks>59%) , then alert(‘invalid clock time’)
or (clks<12%) and (clks<59%) , then accept the number as it is.

if 5 digits: (and all are numbers), then alert(‘invalid clk time’)

These validations need to be done within script tag. (The backend language that I’ve used is jsp.)

Pls help me 🙁

Here is a part of the code that I have written:

<script type='text/javascript'>
function clocks(){

    var clk = document.getElementById('TIME').value;
    var clks = clk.replace('/:/g','');
    var ampm = document.getElementById('AMPM').value;
    var add = 1200;

    if (clks=="")
    {
        alert("You must enter clock time");
    }
    else
    {
        if (ampm=='p')
        {
            clks=parseFloat(clks) + parseFloat(add);
        }
    }

}
....
</script>
  • 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-05-20T00:12:14+00:00Added an answer on May 20, 2026 at 12:12 am

    Here’s a way to do it in a function:

    function validate_time( clks ) {
    
       // Remove any non-digit characters
       clks = clks.toString().replace(/\D/g, '');
       var is_valid = false;
    
       switch (clks.length) {
    
           case 1:
    
               // This will run if the length is 1 digit.
               clks = clks + '00';
    
               // Mark this as a valid time.
               is_valid = true;
    
               // stop running the rest
               break;
    
           case 2:
    
               if ( parseInt(clks) > 12 ) { 
                   alert("Invalid hours value"); 
               } else {
                   is_valid = true;
               }
    
               break;
    
           case 3:
    
               // Get last two characters
               var mins = clks.substr(1,2);    // offset 1 character, length of 2 characters
    
               if ( parseInt(mins) <= 59 ) { 
                   clks = '0' + clks; 
                   is_valid = true;
               } else { 
                   alert('Invalid minutes value'); 
               }
    
               break;
    
           case 4:
    
               var hours = clks.substr(0,2);
               var mins = clks.substr(2,2);
    
               if ( parseInt(hours) > 12 || parseInt(mins) > 59 ) { 
                   alert('Invalid clock time'); 
               } else {
                   is_valid = true;
               }
    
               break;
    
           case 5:
    
               alert("Invalid clock time");
               break;
    
        }
    
        var data = { clks: clks, is_valid: is_valid };
    
        return data;
    
    }
    

    To call it, you’d do:

    var result = validate_time(clks);
    

    and your result would be an object passed back… result.clks is the padded time, and result.is_valid will either be a true or false value as to whether the input time is valid or not.

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

Sidebar

Related Questions

I have a web page with a read-only text box which shows some HTML
I have a text box which accepts time in the format HH:mm and entries
I have the text box which only accepts number and dot so that i
Lets say I have a text box which should have a default value.... and
i have an alert box which i want to show some icelandic text but
I have a textbox which accepts time (max of 5 chars) in the format
I have text box in which your adds values in comma separated values. Once
I have a text box which has a Ajax calendar extender attached. You click
I have a text-box into which a user can input a comment. The comment
At the moment I have a slider and an small input text box which

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.