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:&nbsp;</td>
      <td align="left"><%= text_field :user, :dob, { :size => 10,
                      :onblur => "formatDate(this,'derror')"} %>&nbsp;* DD-MM-YYY&nbsp;<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

Re: Date Validation help

If you use jQuery, get a jQuery plugin like dateFormat,   or if you use prototype or whatever, I'm sure this problem has been solved many times in many ways for your platform.  No sense re-inventing the wheel.

http://plugins.jquery.com/project/jquery-dateFormat

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Date Validation help

BradHodges wrote:

If you use jQuery, get a jQuery plugin like dateFormat,   or if you use prototype or whatever, I'm sure this problem has been solved many times in many ways for your platform.  No sense re-inventing the wheel.

http://plugins.jquery.com/project/jquery-dateFormat

So would this validate input? or would it only allow a certain way of inputting?

Re: Date Validation help

google

jquery validate date plugin

I'm sure you'll find something that does what you want.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Date Validation help

Another way to go, instead of checking that the user has entered a proper format,  make it so he cannot enter an invalid format.  I use jQuery UI,  and in there is a data picker,  it puts up a calendar that you can browse,  the just click on the date you want.   

http://jqueryui.com/demos/datepicker/

Last edited by BradHodges (2011-10-27 12:19:13)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.