0
我想验证表单中的年龄字段,但我遇到了一些问题。首先,我试图确保该字段不会发送空白。我不知道的JavaScript于是我去搜索一些脚本,并将它们适应了这个:验证年龄字段
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 an int number")==false))
{email.focus();return false;}
}
}
//html part
<form action="doador.php" onsubmit="return validate_form(this)" method="post">
它的正常工作为空字段,但是如果我输入年龄字段中的任何字母或字符,它会被发送并保存在我的数据库中为0。谁能帮我?
对不起,有任何英文错误,并提前感谢您的帮助。
下次请缩进您的密码。 – vladv 2010-06-12 18:05:09
非常复杂的功能没有,真的。看看这个其他答案:http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-innumeric – vladv 2010-06-12 18:10:38
此外,我不认为“年龄必须是一个整数”是一个非常有用的消息大多数游客。不要在针对非技术用户的网站上使用像'int'或'integer'这样的技术术语。 – 2010-06-12 18:55:05