Solved

Validate time

Posted on 2004-04-14
7
346 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to access elements of included jsp  from parent jsp's save button 9 22
java beans and EJBs 5 186
console vs log file 16 77
maven set up 2 128
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

861 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

23 Experts available now in Live!

Get 1:1 Help Now