<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Form Validation </title>
<script>
/* Whether with decimals */
function Isdecimal (strvalue) {
var objregexp =/^\d+\.\d+$/;
Return Objregexp.test (strvalue);
}
/* Check whether the Chinese name consists of */
function Ischina (str) {
var reg =/^[\u4e00-\u9fa5]{2,4}$/;
return Reg.test (str);
}
/* Check whether it is made up of 8 digits */
function Isstudentno (str) {
var reg =/^[0-9]{8}$/;
return Reg.test (str);
}
/* Verify Phone code format */
function Istelcode (str) {
var reg =/^ ((0\d{2,3}-\d{7,8}) | ( 1[3584]\D{9})) $/;
return Reg.test (str);
}
/* Verify that the email address is valid */
function Isemail (str) {
var reg =/^ ([a-za-z0-9_-]) [email protected] ([a-za-z0-9_-]) + (\.[ A-za-z0-9_-]) +/;
return Reg.test (str);
}
function Fun () {
if (!isstudentno (document.getElementById ("Sno"). Value)) {
Alert ("Student number is eight digits");
document.getElementById ("Sno"). focus ();
return false;
}
if (!ischina (document.getElementById ("name"). Value)) {
Alert ("Student name must be written in Chinese");
document.getElementById ("name"). focus ();
return false;
}
if (!isemail (document.getElementById ("email"). Value)) {
Alert ("Wrong email address");
document.getElementById ("email"). focus ();
return false;
}
if (!istelcode (document.getElementById ("tel"). Value)) {
Alert ("Wrong phone number");
document.getElementById ("tel"). focus ();
return false;
}
Alert ("Submit success");
return true;
}
</script>
<body>
<form action= "" >
<table border= "1" >
<th colspan= "2" > Student Information Management </th>
<tr>
<td> Student number: <input id = "Sno" type= "text"/> (8 digits) </td>
<td> student Name: <input id= "name" type= "text"/> (Chinese) </td>
</tr>
<tr>
<td> Email Address: <input id= "Email" type= "text"/></td>
<td> Phone: <input id= "Tel" type= "text"/></td>
</tr>
<tr>
<TD colspan= "2" align= "center" ><button type= "Submit" onclick= "Fun ()" > Submit Form </button></td>
</tr>
</table>
</form>
</body>
The
JavaScript regular expression verifies whether the form is legitimate.