Solved

Validate time

Posted on 2004-04-14
7
345 Views
Last Modified: 2010-04-01
How to validate a textbox which is used to enter the time by the user? the format of the time is HH:MM:SS and i want to make sure the user enter the correct format.
0
Comment
Question by:gilliam_ang
  • 4
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 75 total points
Comment Utility
Surprised noone has answered this yet (probably too late now)

Javascript validation:
<script type="text/javascript">
    function validateTime(field) {
        var regex = /^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/;
        if (regex.test(field.value)) {
            alert("valid time");
        } else {
        alert("Please enter time in the format hh:mm:ss (e.g. 05:17:30)");
        }

    }
</script>
<input type="text" name="whatever" onChange="validateTime(this)">



JSP validation:
<%@ page language="java" import="java.util.regex.Pattern" %>
<%
  String strTime = request.getParameter("time");
  boolean validTime = strTime != null && strTime.matches("([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]");
%>


0
 
LVL 7

Assisted Solution

by:searlas
searlas earned 75 total points
Comment Utility
Actually, that import is completely unnecessary for the JSP validation.
JSP validation:
<%
  String strTime = request.getParameter("time");
  boolean validTime = strTime != null && strTime.matches("([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]");
%>
0
 

Author Comment

by:gilliam_ang
Comment Utility
so i can just use the javascript validation will do? If the time is in correct format, it can continue to submit the form... so i can just replace the alert("valid time") to return true; ?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:searlas
Comment Utility
Yes, you can replace the alert.

It depends on your application whether you need anything more than javascript validation.  Bear in mind that a malicious user can read the page source and hand-craft a URL or form-data to send to the server - bypassing any javascript validation.  If there are, or could be, negative consequences of this then you should also employ server side validation.  With validation at both ends you are giving the user quick feedback on their input values (javascript validation) while also protecting the server from malicious users (in this case, JSP validation.)
0
 

Author Comment

by:gilliam_ang
Comment Utility
But the javascript doesn't check when onChange. What's going wrong?
0
 
LVL 7

Expert Comment

by:searlas
Comment Utility
Can you post your HTML?  Validation of forms is usually better done in an onSubmit handler on the form like so:
<script type="text/javascript">
function validate() {
  if (... all fields are valid ...) {
    return true;
  } else {
    return false;
  }
}
</script>
<form name="myform" action="whatever" onSubmit="return validate()">
...

The problem with validating on an onChange is that the contents are only validated the moment they are changed, and not again.  So, if you ignore the warning from an onChange and decide to carry on and submit the form, it will allow it.  However, it serves as a convenient event handler for examples on this forum.

If you're having a problem adapting the ideas to your HTML, post as much of the relevant HTML as you can and I'll see if I can spot the problem.

0
 

Author Comment

by:gilliam_ang
Comment Utility
Can solve already. Thanks.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Spring Tuorial 1 107
Stuck in Bootstrap WysiHtml Editor 3 36
spring AOP 6 71
how to exclude a file using regex 5 116
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now