Validating Age field javascript beginner

Posted by Marcelo on Stack Overflow See other posts from Stack Overflow or by Marcelo
Published on 2010-06-12T18:00:35Z Indexed on 2010/06/12 18:13 UTC
Read the original article Hit count: 288

Filed under:
|
|
|

Hi people, I'm trying to validate an age field in a form but I'm having some problem. First I tried to make sure that the field will not be send empty. I don't know javascript so I searched some scripts and adapted them to this:

function isInteger (s)
{
var i;
if (isEmpty(s))
if (isInteger.arguments.length == 1)
return 0;
else
return (isInteger.arguments[1] == true);
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (!isDigit(c))
return false;
}
return true;
}

function isEmpty(s)
{
return ((s == null) || (s.length == 0))
}

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{
alert(alerttxt);return false;
}
 else
{
return true;
}
}
}

function validate_form(thisform)
{
with (thisform)
{
if  ((validate_required(age,"Age must be filled out!")==false) ||      
isInteger(age,"Age must be and int number")==false))
{email.focus();return false;}
}
}
//html part
<form action="doador.php" onsubmit="return validate_form(this)" method="post">

It's working fine for empty fields, but if I type any letters or characters in age field, it'll be sent and saved a 0 in my database. Can any one help me ?

Sorry for any mistake in English, and thanks in andvance for your help.

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript