Solved

Validate time

Posted on 2004-04-14
7
347 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
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 75 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

For months I had no idea how to 'discover' the IP address of the other end of a link (without asking someone who knows), and it drove me batty. Think about it. You can't use Cisco Discovery Protocol (CDP) because it's not implemented on the ASAs.…
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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