Js judge whether the input is Chinese, numbers, ID cards, and so on. js function set _ javascript skills

Source: Internet
Author: User
We recommend that you search for and collect many common js judgment functions. We recommend that you search for common js judgment functions.

*
Determines whether the specified content is null. If it is null, a warning box is displayed.
*/
Function isEmpty (theValue, strMsg ){
If (theValue = ""){
Alert (strMsg + "cannot be blank! ");
Return true;
}
Return false;
}
/*
Chinese judgment function, which allows the replacement of uncommon words with English "*"
True indicates that the condition is met, and false indicates that the condition is not met.
*/
Function isChinese (str ){
Var badChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
BadChar + = "abcdefghijklmnopqrstuvwxyz ";
BadChar + = "0123456789 ";
BadChar + = "" + ""; // halfwidth and fullwidth Spaces
BadChar + = "'~! @ # $ % ^ & ()-_ = +] \\| :;\" \\' <,>? /"; // It does not contain the English characters * or.
If ("" = str ){
Return false;
}
For (var I = 0; I var c = str. charAt (I); // character in string str
If (badChar. indexOf (c)>-1 ){
Return false;
}
}
Return true;
}
/*
Number judgment function. If true is returned, all digits are returned. If false is returned, not all digits are returned.
*/
Function isNumber (str ){
If ("" = str ){
Return false;
}
Var reg =/\ D /;
Return str. match (reg) = null;
}
/*
Determines whether the given string is a number of the specified length.
True is returned, not false.
*/
Function isNumber_Ex (str, len ){
If ("" = str ){
Return false;
}

If (str. length! = Len ){
Return false;
}

If (! IsNumber (str )){
Return false;
}
Return true;
}

/*
The money judgment function allows the first digit to be "-" to indicate that the account is in arrears.
True indicates that the format is correct. false indicates that the format is incorrect.
*/
Function isMoney (str ){
If ("" = str ){
Return false;
}
For (var I = 0; I var c = str. charAt (I );
If (I = 0 ){
If (c! = "-" & (C <"0" | c> "9 ")){
Return false;
} Else if (c = "-" & str. length = 1 ){
Return false;
}
} Else if (c <"0" | c> "9 "){
Return false;
}
}
Return true;
}
/*
Returns true to indicate all English letters, and false to indicate not all English letters.
*/
Function isLetter (str ){
If ("" = str ){
Return false;
}
For (var I = 0; I var c = str. charAt (I );
If (c <"a" | c> "z") & (c <"A" | c> "Z ")){
Return false;
}
}
Return true;
}
/*
Space determines. If a space is included, false is returned. If a space is not included, true is returned.
"" Cannot be judged
*/
Function notInSpace (str ){
If ("" = str ){
Return false;
}
Var badChar = "";
BadChar + = "";
For (var I = 0; I var c = str. charAt (I); // character in string str
If (badChar. indexOf (c)>-1 ){
Return false;
}
}
Return true;
}
/*
The invoice number judgment function. If the return value is true, the return value is the invoice number. If the return value is false, the return value is invalid.
*/
Function isFPH (str ){
If ("" = str ){
Return false;
}
For (var I = 0; I var c = str. charAt (I );
If (c <"0" | c> "9") & (c! = "-") & (C! = ",")){
Return false;
}
}
Return true;
}

/*
Telephone judgment function, which allows "Number", ";", "-", "(", ")",
True indicates the phone number.
*/
Function isTelephone (str ){
Var trueChar = "()-; 1234567890 ";
If ("" = str ){
Return false;
}
For (var I = 0; I var c = str. charAt (I); // character in string str
If (trueChar. indexOf (c) =-1) return false;
}
Return true;
}
/**
The length can be 1-7, or 3.5, and cannot exceed 7 or less than 1 year.
*/
Function isXZ (str ){
If ("" = str ){
Return false;
}
Var reg =/^ [1-6] (\. 5 )? $ /;
Var r = str. match (reg );
If (null! = R ){
Return true;
} Else {
If (str = "7 "){
Return true;
} Else {
Return false;
}
}
}

/*
Determine whether the certificate number meets the requirements. The certificate number contains Chinese characters, numbers, uppercase/lowercase letters, (,), and ,(,),-
True is returned, not false.
*/
Function isZSBH (str ){
If ("" = str ){
Return false;
}
For (var I = 0; I var c = str. charAt (I );
Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
Alpha + = "abcdefghijklmnopqrstuvwxyz ";
Alpha + = "()-";
If (! IsChinese (c )&&! IsNumber (c) & alpha. indexOf (c) =-1 ){
Return false;
}
}
Return true;
}

/*
ID card judgment function. true is returned, not false.
15 digits, 18 digits or the last digit is X (uppercase)
*/
Function isSFZ (str ){
If ("" = str ){
Return false;
}
If (str. length! = 15 & str. length! = 18) {// incorrect ID card Length
Return false;
}
If (str. length = 15 ){
If (! IsNumber (str )){
Return false;
}
} Else {
Str1 = str. substring (0, 17 );
Str2 = str. substring (17,18 );
Alpha = "X0123456789 ";
If (! IsNumber (str1) | alpha. indexOf (str2) =-1 ){
Return false;
}
}
Return true;
}
/*
Get the year, month, and day of today
Call method: today = new getToday (); then today. year is the year of today, and so on.
*/
Function getToday (){
This. now = new Date ();
This. year = this. now. getFullYear ();
This. month = this. now. getMonth ();
This. day = this. now. getDate ();
}


Js determines numbers, letters, and Chinese characters
1.

Var reg =/^ (\ w | [\ u4E00-\ u9FA5]) * $ /;
If (arr = username. match (reg ))
{
Ti = 1;
Return ture;
}
Else
{
Alert ("the user name can only contain English letters, numbers and Chinese characters. \ n check whether there are spaces or other symbols before and after ");
Ti = 0;
Return false;
}

2. Use regular expressions to restrict text box input in a webpage form:

You can only enter Chinese characters using regular expressions: onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ u4E00-\ u9FA5]/g ,''))"

You can only enter the full-width characters: onkeyup = "value = value. replace (/[^ \ uFF00-\ uFFFF]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ uFF00-\ uFFFF]/g ,''))"

Use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. replace (/[^ \ d]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g ,''))"

You can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g ,''))"


Number
Script
Function check ()
{
If (! IsNaN (document. all. form. str. value ))
{
Alert ("Number ");
}
Script
Letter
Script
Function check ()
{
Var str =/[_ a-zA-Z]/;
If (str. test (document. all. form. str. value ))
{
Alert ("letter ");
}
}
Script






---------------------------------------------------
/**
* Some Common javascript Functions (methods)
*
* For ease of use, all methods written as String objects
* Save it as a. js file to conveniently extend the function of a string object.
*
* Method name
*-------------------------------------------
* Trim deletes the first space.
* Occurs counts the number of times a specified character appears
* IsDigit checks whether it is composed of digits
* IsAlpha checks whether the data is composed of digits, letters, and underscores.
* Check the number of isnumbers
* Lenb returns the number of bytes.
* IsInChinese check for Chinese Characters
* Simple isEmail email check
* IsDate: A simple date check. A date object is returned successfully.
* IsInList check whether there are character characters in the list
* IsInList check whether there are character characters in the list
*/

/*** Delete spaces at the beginning and end ***/
String. prototype. Trim = function (){
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}

/*** Count the number of times a specified character appears ***/
String. prototype. Occurs = function (ch ){
// Var re = eval ("/[^" + ch + "]/g ");
// Return this. replace (re, ""). length;
Return this. split (ch). length-1;
}

/*** Check whether the data is composed of digits ***/
String. prototype. isDigit = function (){
Var s = this. Trim ();
Return (s. replace (/\ d/g, ""). length = 0 );
}

/*** Check whether it is composed of digits, letters, and underscores ***/
String. prototype. isAlpha = function (){
Return (this. replace (/\ w/g, ""). length = 0 );
}
/*** Check the number of items ***/
String. prototype. isNumber = function (){
Var s = this. Trim ();
Return (s. search (/^ [+-]? [0-9.] * $/)> = 0 );
}

/*** Number of returned bytes ***/
String. prototype. lenb = function (){
Return this. replace (/[^ \ x00-\ xff]/g, "**"). length;
}

/*** Check whether Chinese characters are contained ***/
String. prototype. isInChinese = function (){
Return (this. length! = This. replace (/[^ \ x00-\ xff]/g, "**"). length );
}

/*** Simple email check ***/
String. prototype. isEmail = function (){
Var strr;
Var mail = this;
Var re =/(\ w + @ \ w + \. \ w + )(\. {0, 1} \ w *)(\. {0, 1} \ w *)/I;
Re.exe c (mail );
If (RegExp. $3! = "" & RegExp. $3! = "." & RegExp. $2! = ".")
Strr = RegExp. $1 + RegExp. $2 + RegExp. $3;
Else
If (RegExp. $2! = "" & RegExp. $2! = ".")
Strr = RegExp. $1 + RegExp. $2;
Else
Strr = RegExp. $1;
Return (strr = mail );
}

/*** Simple date check. The date object is returned successfully ***/
String. prototype. isDate = function (){
Var p;
Var re1 =/(\ d {4}) [year. /-] (\ d {1, 2}) [month. /-] (\ d {1, 2}) [Day]? $ /;
Var re2 =/(\ d {1, 2}) [month. /-] (\ d {1, 2}) [Day. /-] (\ d {2}) [year]? $ /;
Var re3 =/(\ d {1, 2}) [month. /-] (\ d {1, 2}) [Day. /-] (\ d {4}) [year]? $ /;
If (re1.test (this )){
P = re1.exec (this );
Return new Date (p [1], p [2], p [3]);
}
If (re2.test (this )){
P = re2.exec (this );
Return new Date (p [3], p [1], p [2]);
}
If (re3.test (this )){
P = re3.exec (this );
Return new Date (p [3], p [1], p [2]);
}
Return false;
}
/*** Check whether there are any character characters in the list ***/
String. prototype. isInList = function (list ){
Var re = eval ("/[" + list + "]/");
Return re. test (this );
}
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.