Implementation method of form validation in ASP
In ASP programs, we often ask users to fill in a form and then directly click the send button to send it to us. Today's program limits the content of the form and then sends it to us.
It's just a simple example, I hope it will be useful to everyone in programming.
HTML form:
<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 onClick=check();return false;>
</form>
The Check() function is used to detect the form content.
JavaScript function:
<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>
Just put the above javascript code between <head> </head> of your web page!