Javascript form verification (Part 2)

Source: Internet
Author: User
Tags domain name registration to domain

1. The form item cannot be blank:

<Script language = "java script">
<! --
Function CheckForm ()
{
If (document. form. name. value. length = 0 ){
Alert ("enter your name! ");
Document. form. name. focus ();
Return false;
}
Return true;
}
-->
</Script>

2. Compare whether the values of the two form items are the same:

<Script language = "java script">
<! --
Function CheckForm ()
If (document. form. PWD. value! = Document. form. PWD_Again.value)

{
Alert ("the password you entered twice is different! Enter again .");
Document. ADDUser. PWD. focus ();
Return false;
}
Return true;
}
-->
</Script>

3. Form items can only be numbers and "_", used for phone/Bank Account Verification, and can be extended to domain name registration and so on:

<Script language = "java script">
<! --
Function isNumber (String)
{
Var Letters = "1234567890-"; // you can add input values by yourself.
Var I;
Var c;
If (String. charAt (0) = '-')
Return false;
If (String. charAt (String. length-1) = '-')
Return false;
For (I = 0; I <String. length; I ++)
{
C = String. charAt (I );
If (Letters. indexOf (c) <0)
Return false;
}
Return true;
}
Function CheckForm ()
{
If (! IsNumber (document. form. TEL. value )){
Alert ("your phone number is invalid! ");
Document. form. TEL. focus ();
Return false;
}
Return true;
}
-->
</Script>


4. Input numeric values/length limitation for form items:

<Script language = "java script">
<! --
Function CheckForm ()
{
If (document. form. count. value & gt; 100
| Document. form. count. value <1)
{
Alert ("the input value cannot be less than zero or greater than 100! ");
Document. form. count. focus ();
Return false;
}
If (document. form. MESSAGE. value. length <10)
{
Alert ("the input text is less than 10! ");
Document. form. MESSAGE. focus ();
Return false;
}
Return true;
}
// -->
</Script>


5. Determination of validity of Chinese/English/numbers/email addresses:

<Script LANGUAGE = "java script">
<! --

Function isEnglish (name) // english value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charCodeAt (I) & gt; 128)
Return false;
}
Return true;
}

Function isChinese (name) // Chinese value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charCodeAt (I) & gt; 128)
Return true;
}
Return false;
}

Function isMail (name) // E-mail value detection
{
If (! IsEnglish (name ))
Return false;
I = name. indexOf ("");
J = name dot lastIndexOf ("");
If (I =-1)
Return false;
If (I! = J)
Return false;
If (I = name dot length)
Return false;
Return true;
}

Function isNumber (name) // value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charAt (I) <"0" | name. charAt (I)> "9 ")
Return false;
}
Return true;
}

Function CheckForm ()
{
If (! IsMail (form. Email. value )){
Alert ("your email is invalid! ");
Form. Email. focus ();
Return false;
}
If (! IsEnglish (form. name. value )){
Alert ("the English name is invalid! ");
Form. name. focus ();
Return false;
}
If (! IsChinese (form. cnname. value )){
Alert ("invalid Chinese name! ");
Form. cnname. focus ();
Return false;
}
If (! IsNumber (form. PublicZipCode. value )){
Alert ("the zip code is invalid! ");
Form. PublicZipCode. focus ();
Return false;
}
Return true;
}
// -->
</Script>

 

6. Restrict characters that cannot be entered for form items:

<Script language = "java script">
<! --

Function contain (str, charset) // string contains the test function
{
Var I;
For (I = 0; I <charset. length; I ++)
If (str. indexOf (charset. charAt (I) >=0)
Return true;
Return false;
}

Function CheckForm ()
{
If (contain (document. form. NAME. value, "% \ (\) >< "))
| (Contain (document. form. MESSAGE. value, "% \ (\) >< ")))
{
Alert ("illegal characters entered ");
Document. form. NAME. focus ();
Return false;
}
Return true;
}
// -->
</Script>

  1. Javascript form verification (Part 1)
  2. How to dynamically change the image size through JavaScript
  3. Comprehensive list of practical JavaScript development tools
  4. Programming Language ranking in June: JavaScript is just getting started
  5. The essence of JavaScript Language

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.