熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP中的表單驗證實現方法

2013-11-13 10:11:02  來源: .NET編程 

  在ASP程序中我們經常會要求用戶填寫一個表單然後直接點發送按鈕發送給我們今天這個程序就是限制表單內容然後再發送給我們

  只是一個簡單的實例希望對大家編程有用

  HTML表單

  <form action=actionasp method=post name=myform>
Name and Surname: <br/>
<input type=text name=nameandsurname size=> <br/>
Email: <br/>
<input type=text name=email size=> <br/>
Age: <br/>
<input type=text name=age size=> <br/>
Gender: <br/>
<select size= name=gender>
<option> Select gender </option>
<option value=Male>Male</option>
<option value=Female>Female</option>
</select> <br/>
<input type=submit value=Submit onClick=check();return false;>
</form>

  其中Check()函數是用來檢測表單內容的

  JavaScript函數

  <SCRIPT LANGUAGE=JavaScript TYPE=text/javascript>
function check() {
var nameandsurname = documentmyformnameandsurname;
var email = documentmyformemail;
var age = documentmyformage;
var gender = documentmyformgender;
if ( nameandsurnamevalueindexOf( ) == ) {
alert(Enter your Name and Surname);
nameandsurnamefocus();
} else if ((emailvalueindexOf(@) == ) || (emailvalueindexOf() == )) {
alert(Enter your valid email address);
emailfocus();
} else if (! (parseInt(agevalue) > )) {
alert(Enter your Age);
agefocus();
} else if (genderselectedIndex == ) {
alert(Select your gender);
genderfocus();
} else {
documentmyformsubmit();
}
}
</SCRIPT>

  把上面的javascript代碼放到你的網頁的<head> </head>之間就可以了!


From:http://tw.wingwit.com/Article/program/net/201311/12812.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.