logo

Response

« Return to the main article

You are viewing this page out of context. To see it in the context it is intended please click here.

About This Page

Reply posted by Jake Howlett on Wed 1 May 2002 in response to Form Validator R2.0

What's the chances of that...

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);