Javascript form verification (part 3)

Source: Internet
Author: User

1. check whether a string is composed of digits:

﹤ script language="Java 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 ");
Else alert ("All characters ");




4. Email format Verification:

// Function name: chkemail
// Function Description: Check whether the Email Address is used
// Parameter description: the string to be checked
// Return value: 0: Not 1: Yes
Function chkemail ()
{Var I = a. length;
Var temp = a. indexOf ('@');
Var tempd = a. indexOf ('.');
If (temp> 1 ){
If (I-temp)> 3 ){
If (I-tempd)> 0 ){
Return 1;
}}}
Return 0;
}





5. digit format Verification:

// Function name: fucCheckNUM
// Function Description: Check whether it is a number
// Parameter description: number to be checked
// Return value: 1 is a number, and 0 is not a number
Function fucCheckNUM (NUM)
{
Var I, j, strTemp;
StrTemp = "0123456789 ";
If (NUM. length = 0)
Return 0
For (I = 0; I <NUM. length; I ++)
{
J = strTemp. indexOf (NUM. charAt (I ));
If (j =-1)
{
// It indicates that the character is not a number.
Return 0;
}
}
// The description is a number.
Return 1;
}




6. Phone Number Format Verification:

// Function name: fucCheckTEL
// Function Description: Check whether the phone number is used
// Parameter description: the string to be checked
// Return value: 1 is valid, 0 is invalid
Function fucCheckTEL (TEL)
{
Var I, j, strTemp;
StrTemp = "0123456789 -()#";
For (I = 0; I <TEL. length; I ++)
{
J = strTemp. indexOf (TEL. charAt (I ));
If (j =-1)
{
// The specified character is invalid.
Return 0;
}
}
// Valid description
Return 1;
}



7. Function for determining whether the input is Chinese:

function ischinese(s){   
var ret=true;  
for(var i=0;i﹤s.length;i++)  
ret=ret && (s.charCodeAt(i)﹥=10000);  
return ret;  
}   



8. Comprehensive function for determining the legality of user input:

<Script language = "java script">
// Limit the start of the number of characters
// M is the user input, and n is the number of digits to be limited
Function issmall (m, n)
{
If (m <n) & (m> 0 ))
{
Return (false );
}
Else
{Return (true );}
}



9. Check whether the password is consistent:

function issame(str1,str2)  

if (str1==str2) 
{return(true);} 
else 
{return(false);} 



10. Determine whether the user name is a hyphen with numbers or letters:

function notchinese(str){ 
var reg=/[^A-Za-z0-9_]/g
if (reg.test(str)){
return (false);
}else{
return(true);     }
}


11. Common verification functions for form text fields:

Purpose: Check all input texts that must not be empty, such as names, accounts, and email addresses.
This verification is only applicable to text fields. If you want to target other domain objects in form, you can change the judgment conditions.

Usage: add the title text to the text field to be checked. Text is in the prompt message,
The Chinese name of the field you want to prompt the user. For example, to check the user name
Html: <input name = "txt_1" title = "name">. Of course, it is best to use a visualization tool.
For example, dreamweaver or something to edit a domain.
If you want to detect data of the numeric type, the id of the domain is unified to sz.
Java script is difficult to judge the date type, so there is no program for date type verification.

Program comparison, just provide a train of thought. Let me know! :)

Function call method:

<Form onsubmit = "return dovalidate ()">

Function dovalidate ()
{
Fm = document. forms [0] // detects only one form. If there are multiple forms, the judgment conditions can be changed.
For (I = 0; I <fm. length; I ++)
{
// Detection and judgment conditions, which can be modified Based on Different Types
If (fm [I]. tagName. toUpperCase () = "INPUT" & fm [I]. type.
ToUpperCase () = "TEXT" & (fm [I]. title! = ""))

If (fm [I]. value = "/blog/= "")//
{
Str_warn1 = fm [I]. title + "cannot be blank! ";
Alert (str_warn1 );
Fm [I]. focus ();
Return false;
}
If (fm [I]. id. toUpperCase () = "SZ") // digit Verification
{
If (isNaN (fm [I]. value ))
{Str_warn2 = fm [I]. title + "Incorrect format ";
Alert (str_warn2 );
Fm [I]. focus ();
Return false;
}
}
}
Return true;
}


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

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.