Javascript form verification control code

Source: Internet
Author: User
Tags domain name registration to domain
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 name = A onsubmit = "return test ()">
<Textarea name = "B" Cols = "40" Wrap = "virtual" rows = "6"> </textarea>
<Input type = "Submit" name = "Submit" value = "check">
</Form>
2. Only Chinese characters are allowed
<Input onkeyup = "value ="/oblog/value. Replace (/[^ \ u4e00-\ u9fa5]/g, '')">
3. English only
<Script language = JavaScript>
Function onlyeng ()
{
If (! (Event. keycode> = 65 & event. keycode <= 90 ))
Event. returnvalue = false;
}
</SCRIPT>
<Input onkeydown = "onlyeng ();">
4. Only numbers are allowed.
<Script language = JavaScript>
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 onkeydown = "onlynum ();">
5. Only English characters and numbers are allowed.
<Input onkeyup = "value ="/oblog/value. Replace (/[\ W]/g ,"'')"
Onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^ \ D]/g,'') ">
6. Verify the email format
<Script language = JavaScript 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 type = text onblur = isemail (this. Value)>
7. Shielding keywords (here, shielding ***** and ****)
<Script language = "javascript1.2">
Function Test (){
If (. b. value. indexof ("***") = 0) | (. b. value. indexof ("***") = 0 )){
Alert (":)");
A. B. Focus ();
Return false ;}
}
</SCRIPT>
<Form name = A onsubmit = "return test ()">
<Input type = text name = B>
<Input type = "Submit" name = "Submit" value = "check">
</Form>
8. Whether the two passwords are the same
<Form method = post action = "">
<Input type = "password" id = "input1">
<Input type = "password" id = "input2">
<Input type = "button" value = "test" onclick = "check ()">
</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.
9. Blocking right-click is cool
Oncontextmenu = "Return false" ondragstart = "Return false" onselectstart = "Return false"
Add to Body

II
2.1 Form items cannot be blank
<Script language = "JavaScript">
<! --
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 language = "JavaScript">
<! --
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 language = "JavaScript">
<! --
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 language = "JavaScript">
<! --
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>

 

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.