Link to home
Start Free TrialLog in
Avatar of gilliam_ang
gilliam_ang

asked on

Validate time

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.
ASKER CERTIFIED SOLUTION
Avatar of searlas
searlas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gilliam_ang
gilliam_ang

ASKER

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; ?
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.)
But the javascript doesn't check when onChange. What's going wrong?
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.

Can solve already. Thanks.