入門:ASP中的表單驗證實現方法

來源:互聯網
上載者:User
表單驗證

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

  只是一個簡單的執行個體,希望對大家編程有用。

  HTML表單:

<form action="action.asp" method="post" name="myform">
Name and Surname: <br/>
<input type="text" name="nameandsurname" size="30"> <br/>
Email: <br/>
<input type="text" name="email" size="30"> <br/>
Age: <br/>
<input type="text" name="age" size="3"> <br/>
Gender: <br/>
<select size="1" name="gender">
<option>- Select gender -</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select> <br/>
<input type="submit" value="Submit" >
</form>

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

  JavaScript函數:

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
function check() {
var nameandsurname = document.myform.nameandsurname;
var email = document.myform.email;
var age = document.myform.age;
var gender = document.myform.gender;
if ( nameandsurname.value.indexOf(" ") == -1) {
alert("Enter your Name and Surname.");
nameandsurname.focus();
} else if ((email.value.indexOf("@") == -1) || (email.value.indexOf(".") == -1)) {
alert("Enter your valid email address.");
email.focus();
} else if (! (parseInt(age.value) > 0)) {
alert("Enter your Age.");
age.focus();
} else if (gender.selectedIndex == 0) {
alert("Select your gender.");
gender.focus();
} else {
document.myform.submit();
}
}
</SCRIPT>

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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.