js几个验证函数代码
程序员文章站
2023-12-11 23:29:16
复制代码 代码如下: //检查是否非空 function notempty(obj, msg) { str = obj.value; str1 = ""; for (i =...
复制代码 代码如下:
//检查是否非空
function notempty(obj, msg)
{
str = obj.value;
str1 = "";
for (i = 0; i < str.length; i++)
{
if (str.charat(i) != " ")
{
str1 = str.substr(i, str.length);
break;
}
}
if (str1 == "")
{
alert(msg);
obj.value = "";
obj.focus();
return false;
}
else
{
return true;
}
}
//检查是否为数字
function isnumber(obj, msg)
{
if(isnan(obj.value))
{
if (undefined == msg)
{
msg = "请输入数字!";
}
alert(msg);
obj.select();
return false;
}
else
{
return true;
}
}
//检查密码是否相同
function issamepwd(objpwd1, objpwd2, msg)
{
pwd1 = objpwd1.value;
pwd2 = objpwd2.value;
if (pwd1 != pwd2)
{
if (null == msg)
{
alert("密码不相同!");
}
else
{
alert(msg);
}
objpwd2.value = "";
objpwd2.focus();
return false;
}
else
{
return true;
}
}
//检查邮件地址
function isemail(obj, msg)
{
ch = obj.value;
if((ch.indexof("@") < 1) || (ch.indexof(".") < 1) || (ch.indexof(".") == ch.length - 1))
{
if (null == msg)
{
alert("email不正确!");
}
else
{
alert(msg);
}
obj.select();
return false;
}
else
{
return true;
}
}
复制代码 代码如下:
<script language="javascript">
function checkspace(checkstr) {
var str = '';
for(i = 0; i < checkstr.length; i++) {
str = str + ' ';
}
return (str == checkstr);
}
function checkfrm()
{
if (checkspace(document.frm.title.value))
{
alert('标题不能为空');
document.frm.title.focus();
return false;
}
if (checkspace(document.frm.truename.value))
{
alert("姓名必须填写");
document.frm.truename.focus();
return false;
}
if (checkspace(document.frm.danwei.value))
{
alert("单位名称没有填写");
document.frm.dianwei.focus();
return false;
}
if (checkspace(document.frm.dizhi.value))
{
alert("地址不能为空");
document.frm.dizhi.focus();
return false;
}
if (checkspace(document.frm.content.value))
{
alert("留言内容部能为空");
document.frm.content.focus();
return false;
}
if (checkspace(document.frm.email.value))
{
alert("邮箱不能为空");
document.frm.email.focus();
return false;
}
return true;
}
</script>