Topic: Date Validation help
Trying to write the Javascript to validate the date field for only date of birth in the DD-MM_YYYY format with the dashes
The view
-------------------------------------------------------
<tr>
<td align="right">Dob: </td>
<td align="left"><%= text_field :user, :dob, { :size => 10,
:onblur => "formatDate(this,'derror')"} %> * DD-MM-YYY <span id="derror" style="float:left;display:none;color:red;">Invalid Dob (DD-MM-YYYY)</span>
</td>
</tr>
------------------------------------------------------------------
js
-----------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------
/* tests to see if string is in correct UK style date: DD-MM-YYYY. */
function isValidDate(p) {
var dateRegEx = /^\d{2}|-|\d{2}|-|\d{4}/;
return dateRegEx.test(p) && p.length <= 8;
}
//--------------------------------------------------------------------------------------------------------------
/* formats a VALID dob: DD-MM-YYYY */
function formatDate(o,nve) {
p = o.value.toUpperCase();
if (isValidDate(p)) {
var dateRegEx = /^\d{2}|-|\d{2}|-|\d{4}/;
if (nve.length) $(nve).hide();
return o.value = p.replace(dateRegEx,"$1 $2");
} else {
new Effect.Highlight(o, {startcolor: '#ff0000', restorecolor: 'true'}) //Flash it RED if not valid
if (nve.length) $(nve).show();
return false;
}
}
------------------------------------------------------------------------------------
Not the best at JavaScript so any help would be great
Thanks