在asp.net调用javascript的步骤
第一步:在asp.net页面写javascript方法
放在<body><form>之间
<scriptlanguage="javascript"type="text/javascript">
functionvalidate()
{
if(document.getElementById("txtName").value=="")
{
alert("NameNotFound");
document.getElementById("txtname").focus();
returnfalse;
}
if(document.getElementById("txtemail").value=="")
{
alert("EmailCantNotFind");
document.getElementById("txtEmail").focus();
returnfalse;
}
varemailPat="^[a-zA-Z0-9_.]+@[a-zA-Z0-9-]+[.a-zA-Z]+$";
varemailID=document.getElementById("txtEmail").value;
varmatchArray=emailID.match(emailPat);
if(matchArray==null)
{
alert('Youremailaddressseemsincorrect.Pleasetryagain.');
document.getElementById("txtEmail").focus();
returnfalse;
}
vardigits="0123456789";
vartemp;
for(i=0;document.getElementById("txtZip").value.length;i++)
{
temp=document.getElementById("txtZip").value.substring(i,i+1);
if(digits.indexOf(temp)==-1)
{
alert("pleaseentercurrentzipcode");
document.getElementById("txtZip").focus();
returnfalse;
}
}
returntrue;
}
</script>
functionvalidate()
{
if(document.getElementById("txtName").value=="")
{
alert("NameNotFound");
document.getElementById("txtname").focus();
returnfalse;
}
if(document.getElementById("txtemail").value=="")
{
alert("EmailCantNotFind");
document.getElementById("txtEmail").focus();
returnfalse;
}
varemailPat="^[a-zA-Z0-9_.]+@[a-zA-Z0-9-]+[.a-zA-Z]+$";
varemailID=document.getElementById("txtEmail").value;
varmatchArray=emailID.match(emailPat);
if(matchArray==null)
{
alert('Youremailaddressseemsincorrect.Pleasetryagain.');
document.getElementById("txtEmail").focus();
returnfalse;
}
vardigits="0123456789";
vartemp;
for(i=0;document.getElementById("txtZip").value.length;i++)
{
temp=document.getElementById("txtZip").value.substring(i,i+1);
if(digits.indexOf(temp)==-1)
{
alert("pleaseentercurrentzipcode");
document.getElementById("txtZip").focus();
returnfalse;
}
}
returntrue;
}
</script>
第二步 就是 .cs中的 Page_load 事件 添加如下代码:
protectedvoidPage_Load(objectsender,EventArgse)
{
btnSubmit.Attributes.Add("onclick","returnvalidate()");
}
{
btnSubmit.Attributes.Add("onclick","returnvalidate()");
}
本文地址:http://www.45fan.com/a/question/70461.html