JavaScript best Form Verification

Source: Internet
Author: User

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> registered member </title>
<Style type = "text/css">
. Table {
Border-collapse: collapse;
Font-size: 12px;
}
. Input {
Width: 200px;
Height: 22px;
}
. Stats1 {
Color: # aaa;
}
. Stats2 {
Color: #000;
}
. Stats3 {
Color: red;
}
. Stats4 {
Color: green;
}
</Style>
</Head>
<Body>
<Form action = "index. php" method = "post" onSubmit = "return regCheck ('click')">
<Table border = "0" align = "center" width = "500" class = "table">
<Tr>
<Td width = "80" height = "25" align = "right"> User Name: </td> <input type = "text" name = "username" value = "" class = "input"/> <span class = "stats1"> </ span> </td>
</Tr>
<Tr>
<Td width = "80" height = "25" align = "right"> password: </td> <input type = "password" name = "password" value = "" class = "input"/> <span class = "stats1"> </ span> </td>
</Tr>
<Tr>
<Td width = "80" height = "25" align = "right"> confirm the password: </td> <input type = "password" name = "repass" value = "" class = "input"/> <span class = "stats1"> </ span> </td>
</Tr>
<Tr>
<Td width = "80" height = "25" align = "right"> Email: </td> <input type = "text" name = "email" value = "" class = "input"/> <span class = "stats1"> </ span> </td>
</Tr>
<Tr>
<Td height = "30"> </td> <td align = "left"> <input type = "submit" name = "send" value = "submit"/> <input type = "reset" value = "reset"/> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>
<Script language = "javascript">
// Locate the SPAN tag after input
Function gspan (cobj ){
While (true ){
If (cobj. nextSibling. nodeName! = "SPAN ")
Cobj = cobj. nextSibling;
Else
Return cobj. nextSibling;
}
}
 
Function check (obj, info, fun, click ){
Var sp = gspan (obj );
Obj. onfocus = function (){
Sp. innerHTML = info;
Sp. className = "stats2 ";

}
 
Obj. onblur = function (){
If (fun (this. value )){
Sp. innerHTML = "correct input √ ";
Sp. className = "stats4 ";

} Else {
Sp. innerHTML = info;
Sp. className = "stats3 ";
}
}
 
If (click = "click ")
Obj. onblur ();
}
 
// Automatic call after page loading
Onload = regCheck
Function regCheck (click ){
 
Var stat = true;
Var username = document. getElementsByName ("username") [0];
Var password = document. getElementsByName ("password") [0];
Var repass = document. getElementsByName ("repass") [0];
Var email = document. getElementsByName ("email") [0];
Var other = document. getElementsByName ("other") [0];
 
// Verify the user name
Check (username, "username is a 4-20-digit English letter or number! ", Function (val ){
 
If (val. match (/^ \ S + $/) & val. length> = 4 & val. length <= 20 ){
Return true;
} Else {
Stat = false;
Return false;
}
}, Click );

// Verify the password
Check (password, "the user password must be between 6 and 20 characters! ", Function (val ){
If (val. match (/^ \ S + $/) & val. length> = 6 & val. length <= 20 ){
Return true;
} Else {
Stat = false;
Return false;
}
}, Click );
 
// Verify the password
Check (repass, "Make sure the password is the same as above! ", Function (val ){
If (val. match (/^ \ S + $/) & val. length> = 6 & val. length <= 20 & val = password. value ){
Return true;
} Else {
Stat = false;
Return false;
}
}, Click)
 
// Verify Email
Check (email, "enter an email in the correct format! ", Function (val ){
If (val. match (/\ w + @ \ w + \. \ w /)){
Return true;
} Else {
Stat = false;
Return false;
}
}, Click)
 
Return stat;
}
</Script>
 

From Lee.'s column

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.