Reusable Function problem

N

Nacho

Hello..

I have one problem with my reusable function to validate date..

I want to use this function to all my web project no validate date, but
It's works fine if I have one textbox to validate in the same form, If
I have two textbox the return doesn't works correctly.. how can I do
this?

if (document.Form1.TxtFechaIng.value!=''){
return validafch(document.Form1.TxtFechaIng.value);
}
if (document.Form1.txtFchNaci.value!=''){
return validafch(document.Form1.txtFchNaci.value);
}

thanks
nacho

this is my function:

function anyoBisiesto(anyo)
{
var anyo;
if (anyo < 100)
var fin = anyo + 1900;
else
var fin = anyo ;

if (fin % 4 != 0)
return false;
else
{
if (fin % 100 == 0)
{
if (fin % 400 == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}
}

function validafch(fecha){
var a, mes, dia, anyo, febrero,regresa;
a=fecha;
dia=a.split('/')[0];
mes=a.split('/')[1];
anyo=a.split('/')[2];
if ((isNaN(dia)==true) || (isNaN(mes)==true) ||
(isNaN(anyo)==true))
{
alert('La fecha introducida debe estar formada solo por numeros');
regresa=1;
}
if (anyoBisiesto(anyo))
febrero=29;
else
febrero=28;
if ((mes<1) || (mes>12))
{
alert('El mes introducido no es valido. Por favor, introduzca un
mes correcto');
regresa=1;
}
if ((mes==2) && ((dia<1) || (dia>febrero)))
{
alert('El dia introducido no es valido. Por favor, introduzca un
dia correcto');
regresa=1;
}
if (((mes==1) || (mes==3) || (mes==5) || (mes==7) || (mes==8) ||
(mes==10) || (mes==12)) && ((dia<1) || (dia>31)))
{
alert('El dia introducido no es valido. Por favor, introduzca un
dia correcto');
regresa=1;
}
if (((mes==4) || (mes==6) || (mes==9) || (mes==11)) && ((dia<1) ||
(dia>30)))
{
alert('El dia introducido no es valido. Por favor, introduzca un
dia correcto');
regresa=1;
}
if ((anyo<1900) || (anyo>2010))
{
alert('El año introducido no es valido.');
regresa=1;
}
if (regresa==1)
{return false} <---this is the problem
else{return true}
}
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 14 Jun 2006 08:21:37 remote, seen in
news:comp.lang.javascript said:
I have one problem with my reusable function to validate date..

An underestimate.

Your function is far longer than is needed.

You should read the newsgroup FAQ, to discover how to format posts for
legibility and where to look for other information you need.

this is my function:

function anyoBisiesto(anyo)
{
var anyo;
if (anyo < 100)
var fin = anyo + 1900;
else
var fin = anyo ;

That fails for current dates in browsers that return the equivalent of
getFullYear%100 for getYear; such have been said to exist. It also
fails if a two-digit year is typed for a current date.
if (fin % 4 != 0)
return false;
else
{
if (fin % 100 == 0)
{
if (fin % 400 == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}
}

That function is not necessary anyway. Else is not needed after return.
function validafch(fecha){
var a, mes, dia, anyo, febrero,regresa;
a=fecha;
dia=a.split('/')[0];
mes=a.split('/')[1];
anyo=a.split('/')[2];

You call split three times; you should call it once and save the value.

Your dia mes anyo are Strings; but Numbers would be better. Use
a = fecha.split('/') ; dia = +a[0] ; ... to get Numbers.
if ((isNaN(dia)==true) || (isNaN(mes)==true) ||
(isNaN(anyo)==true))

'==true' is superfluous.
if (isNaN(dia+mes+anyo)) should suffice, with Numbers.
{
alert('La fecha introducida debe estar formada
solo por numeros');

You should not allow your posting agent to line-wrap code. It is better
to indent in smaller units, such as two spaces, for News. Code as
received should be directly executable, and readable in < 80 columns.
regresa=1;
}
if (anyoBisiesto(anyo))
febrero=29;
else
febrero=28;

Could be febrero = anyoBisiesto(anyo) ? 29 : 28 but not needed.
if ((mes<1) || (mes>12))
{
alert('El mes introducido no es valido. Por
favor, introduzca un
mes correcto');
regresa=1;
}
if ((mes==2) && ((dia<1) || (dia>febrero)))
{
alert('El dia introducido no es valido. Por
favor, introduzca un
dia correcto');

Better to check dia<1 once only, for any month, but not needed.
regresa=1;
}
if (((mes==1) || (mes==3) || (mes==5) ||
(mes==7) || (mes==8) ||
(mes==10) || (mes==12)) && ((dia<1) || (dia>31)))
{
alert('El dia introducido no es valido. Por
favor, introduzca un
dia correcto');
regresa=1;
}
if (((mes==4) || (mes==6) || (mes==9) ||
(mes==11)) && ((dia<1) ||
(dia>30)))
{
alert('El dia introducido no es valido. Por
favor, introduzca un
dia correcto');
regresa=1;
}
if ((anyo<1900) || (anyo>2010))

Change the bottom limit to 1901, and two Leap Year rules are not needed.
{
alert('El año introducido no es valido.');
regresa=1;
}

Note : regresa is either undefined or 1. That's not good practice; set
it to false at declaration, to true at error, and return it.
if (regresa==1)
{return false} <---this is the problem
else{return true}

return regresa==1 is simpler.


See <URL:http://www.merlyn.demon.co.uk/js-dates.htm>,
<URL:http://www.merlyn.demon.co.uk/js-date4.htm> .

The calendar is fairly well known. Remember that every byte of your
code is transmitted to every user. Therefore, if a date given is not a
valid Gregorian date, I see no need to describe its defect(s) in detail.
We all know that a year should normally be a positive integer, a month
should be in 1..12, and a day in 1..28-31; mistakes can only be typos or
month-length errors.


To validate a numeric date string, use a RegExp to check the pattern and
isolate the fields. Load the fields into a Date Object, read back the
day and month, and compare with the originals.


function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (new Date(y, m, d)) return (getMonth()==m && getDate()==d) }


function GetYMDdateC(Q) { var d, m, D, Undef // year!=0, >99?
if ((D = /^(\d+)(\D)(\d\d)(\2)(\d\d)$/.exec(Q)) != null)
with (D = new Date(D[1], m=D[3]-1, d=+D[5]))
if (getMonth()==m && getDate()==d) return D
return Undef }

// ? return Undef is not *really* needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top