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