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
Solved

Validate time

Posted on 2004-04-14
7
348 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
method inner class 6 83
AlarmClock Challenge 35 155
How to find missing packages when using Netbeans IDE 8.1 ? 19 59
mask rule in struts validation.xml 5 94
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
This article describes how to reset your Windows 10 password when you've forgotten it.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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