logo

New Response

« Return to the main article

You are replying to:

  1. Only today I wrote a few lines to validate a time field (something I'd never done before).

    I used a different approach and for a field where there was no AM/PM but was in 24 hr format and didn't require leading 0.

    I added a method called "checkTimeFormat()" to the prototype object of all Strings like so: [<pre class="JavaScript">]String.prototype.checkTimeFormat = function ( ){ return /^[0-2]?\d:[0-5]{1}\d/.test( this ); }[</pre>] A call to which looks like: [<pre class="JavaScript">]frm = document.forms[0]; if( !frm.TimeField.value.checkTimeFormat() ){ alert('Dodgy time!') }[</pre>] Just thought I'd share it with you. As you have so kindly done. Cheers

    Jake PS: As your script stands I could get away with entering 39:99 PM ! PPS: If you pass the field's object to the function there is no need for:

    var textTime = document.myForm.elements[timeV.name].value;

    as you can just use:

    var textTime = timeV.value;

    PPPS: Instead of checking for AM and am you can use toLowerCase() and then it's:

    var valresult = textTime.toLowerCase().match(timeExpression);

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment: