PDA

View Full Version : Date of birth - form validation


allyson
09-02-2002, 12:02 PM
Hi

The following code validates the Date of Birth on a form, in the layout of mm/dd/yyyy. How could I modify the format to dd/mm/yyyy? Can anyone help, please?





function validDate(formField,fieldLabel,required)
{
var result = true;

if (required && !validRequired(formField,fieldLabel))
result = false;

if (result)
{
var elems = formField.value.split("/");

result = (elems.length == 3); // should be three components

if (result)
{
var month = parseInt(elems[0]);
var day = parseInt(elems[1]);
var year = parseInt(elems[2]);
result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
allDigits(elems[1]) && (day > 0) && (day < 32) &&
allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
}

if (!result)
{
alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
formField.focus();
}
}



=--------
bye
Allyson

mordred
09-02-2002, 02:25 PM
If that's the only thing, you can try to rearrange the array elements a little bit


var month = parseInt(elems[1], 10);
var day = parseInt(elems[0], 10);
var year = parseInt(elems[2], 10);


Note that I added the optional radix parameter - that makes sure your code does also parse correctly a string like "09" without trying to create an octal number (which is not possible for "09").