JavaScript Regular Expressions Verify that the form is legitimate.

Source: Internet
Author: User
Tags button type

<! 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>
&LT;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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.