第一步:在asp.net頁面寫javascript方法
放在<body><form>之間
<script language="javascript" type="text/javascript">
function validate()
...{
if (document.getElementById("txtName").value=="")
...{
alert(" Name Not Found ");
document.getElementById("txtname").focus();
return false;
}
if (document.getElementById("txtemail").value=="")
...{
alert("Email Cant Not Find");
document.getElementById("txtEmail").focus();
return false;
}
var emailPat="^[a-zA-Z0-9_.]+@[a-zA-Z0-9-]+[.a-zA-Z]+$";
var emailID=document.getElementById("txtEmail").value;
var matchArray=emailID.match(emailPat);
if (matchArray==null)
...{
alert(‘Your email address seems incorrect. Please try again.‘);
document.getElementById("txtEmail").focus();
return false;
}
var digits="0123456789";
var temp;
for(i=0;document.getElementById("txtZip").value.length;i++)
...{
temp=document.getElementById("txtZip").value.substring(i,i+1);
if (digits.indexOf(temp)==-1)
...{
alert("please enter current zip code");
document.getElementById("txtZip").focus();
return false;
}
}
return true;
}
</script>
第二步 就是 .cs中的 Page_load 事件 添加如下代碼:
protected void Page_Load(object sender, EventArgs e)
...{
btnSubmit.Attributes.Add("onclick", "return validate()");
} 這樣就可以實(shí)現(xiàn) 用javascript 進(jìn)行驗(yàn)證了