?
Solved

Validate time

Posted on 2004-04-14
7
Medium Priority
?
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 150 total points
ID: 10876342
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 150 total points
ID: 10876348
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
ID: 10885609
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:searlas
ID: 10886706
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
ID: 10896091
But the javascript doesn't check when onChange. What's going wrong?
0
 
LVL 7

Expert Comment

by:searlas
ID: 10897328
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
ID: 10925982
Can solve already. Thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 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