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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.)
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.)
ASKER
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.
<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.
ASKER
Can solve already. Thanks.
ASKER