Simple JS Form Verification

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

JS verification form Overview
1. length limit
<SCRIPT>
Function Test ()
{
If (document. A. B. value. length> 50)
{
Alert ("cannot exceed 50 characters! ");
Document. A. B. Focus ();
Return false;
}
}
</SCRIPT>
<Form test () ">
<Textarea Cols = "40" Wrap = "virtual" rows = "6"> </textarea>
<Input value = "check">
</Form>

2. Only Chinese characters are allowed
<Input/oblog/value. Replace (/[^ \ u4e00-\ u9fa5]/g, '')">

3. "English only
<SCRIPT>
Function onlyeng ()
{
If (! (Event. keycode> = 65 & event. keycode <= 90 ))
Event. returnvalue = false;
}
</SCRIPT>

<Input>

4. Only numbers are allowed.
<SCRIPT>
Function onlynum ()
{
If (! (Event. keycode> = 48 & event. keycode <= 57) | (event. keycode> = 96 & event. keycode <= 105 )))
// Consider the numeric keys on the keypad
Event. returnvalue = false;
}
</SCRIPT>

<Input>

5. Only English characters and numbers are allowed.
<Input/oblog/value. replace (/[\ W]/g, "'') "onbeforepaste =" clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g, '')">

6. Verify the fuel tank format
<SCRIPT runat = Server>
Function isemail (stremail ){
If (stremail. search (/^ \ W + (-\ W +) | (\. \ W +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ /)! =-1)
Return true;
Else
Alert ("oh ");
}
</SCRIPT>
<Input>
7. Shielding keywords (here, shielding ***** and ****)
<SCRIPT>
Function Test (){
If (. b. value. indexof ("***") = 0) | (. b. value. indexof ("***") = 0 )){
Alert (":)");
A. B. Focus ();
Return false ;}
}
</SCRIPT>
<Form test () ">
<Input> <input value = "check">
</Form>

8. Whether the two passwords are the same
<Form method = post action = "">
<Input>
<Input>
<Input value = "test">
</Form>
<SCRIPT>
Function check ()
{
With (document. All ){
If (input1.value! = Input2.value)
{
Alert ("false ")
Input1.value = "";
Input2.value = "";
}
Else document. Forms [0]. Submit ();
}
}
</SCRIPT>
Enough.
Blocking right-click is cool
Oncontextmenu = "Return false"
Add to Body

II

2.1 Form items cannot be blank

<SCRIPT>
<! --
Function checkform ()
{
If (document. Form. Name. value. Length = 0 ){
Alert ("enter your name! ");
Document. Form. Name. Focus ();
Return false;
}
Return true;
}
-->
</SCRIPT>

2.2 compare whether the values of the two form items are the same

<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>

2.3 form items can only be numbers and "_", used for phone/Bank Account Verification, and can be extended to domain name registration.

<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>

2.4 form entry input value/length limit

<SCRIPT>
<! --
Function checkform ()
{
If (document. Form. Count. value> 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>

2.5 Determination of validity of Chinese/English/numbers/email addresses

<SCRIPT>
<! --

Function isenglish (name) // english value detection
{
If (name. Length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charcodeat (I)> 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)> 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>

2.6 restrict characters that cannot be entered for Form Items

<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. check whether a string is composed of digits.
---------------------------------------
<SCRIPT> <! --
Function checknum (STR) {return Str. Match (/\ D/) = NULL}
Alert (checknum ("1232142141 "))
Alert (checknum ("123214214a1 "))
// --> </SCRIPT>

2. How to determine whether it is a character
---------------------------------------
If (/[^ \ x00-\ xFF]/g. test (s) Alert ("containing Chinese characters ");
Else alert ("All characters ");

3. How to determine whether Chinese characters are contained
---------------------------------------
If (escape (STR). indexof ("% u ")! =-1) Alert ("containing Chinese characters ");

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.