A regular expression (instance) that determines the number \ Letter \ Chinese in JS _javascript tips

Source: Internet
Author: User
/*
To determine if the specified content is empty, or to eject the warning box if blank
*/
Function IsEmpty (thevalue, STRMSG) {
if (thevalue== "") {
Alert (strmsg+ "cannot be empty!");
return true;
}
return false;
}
/*
Chinese judgment function, allow rare words in English "*" instead
Returns true to indicate that it is eligible and returns false that does not conform
*/
function Ischinese (str) {
var Badchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Badchar + = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Badchar + = "0123456789";
Badchar + = "" + "";//Half-width and full-width spaces
Badchar + = "Www.jb51.net does not contain * or. English symbols
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;
}
/*
Numeric judgment function, returns true to be all numbers, and returns false to not all numbers
*/
function Isnumber (str) {
if ("" ==str) {
return false;
}
var reg =/\d/;
Return Str.match (reg) ==null;
}
/*
Determines whether a given string is a number of a specified length
is to return true, not return false
*/
function Isnumber_ex (Str,len) {
if ("" ==str) {
return false;
}
if (Str.length!=len) {
return false;
}
if (!isnumber (str)) {
return false;
}
return true;
}
/*
Money judgment function that allows the first one to "-" to represent a debt owed
Returns true to indicate that the format is correct and returns false for formatting errors
*/
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;
}
/*
The English judgment function, returns True is all English, returns false to indicate not all is English
*/
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 to judge, when containing a space to return false, when does not contain a space to return true
"" 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 * * Number judgment function, returns True is the * * number, returns false to indicate does not conform to the specification
*/
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;
}
/*
A telephone judgment function that 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 academic system can be 1-7, or 3.5 of this form, not more than 7 years or less than 1 years
*/
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;
}
}
}
/*
To determine if the certificate number meets the requirements, the certificate number contains Chinese, numeric, uppercase and lowercase letters, (,),-
is to return true, not return 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;
}
/*
Identity card function, is to return true, not return false
15 digits, 18 digits, or the last one is X (uppercase)
*/
function Issfz (str) {
if ("" ==str) {
return false;
}
if (str.length!=15&&str.length!=18) {//ID card length is incorrect
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 today's year, month, day
Call Method: Today = new Gettoday (); Then today.year for today's year 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 judgment number, letter, Chinese
1.
var reg =/^ (\w|[ \U4E00-\U9FA5]) *$/;
if (Arr=username.match (REG))
{
Ti=1;
return ture;
}
Else
{
Alert ("User name is only allowed in English, numbers and Chinese characters, \ n Please check whether there are spaces or other symbols before and after");
ti=0;
return false;
}
2. Use regular expressions to restrict the entry of text boxes in a Web page's form:
The regular expression limit can only be entered in Chinese: onkeyup= "value=value.replace (/[^\u4e00-\u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "
Only full-width characters can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g,") "Onbeforepaste=" Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ') "
Only numbers can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "
Only numbers and English can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "

Digital
<script>
function Check ()
{
if (!isnan (Document.all.form.str.value))
{
Alert ("number");
}
</script>
Letters
<script>
function Check ()
{
var str =/[_a-za-z]/;
if (Str.test (Document.all.form.str.value))
{
Alert ("letter");
}
}
</script>
<form name= "form" action= "onsubmit=" return check (); " >
<input Type=text name=str>
<input type=submit>
<form>
---------------------------------------------------
/**
* Some common JavaScript functions (methods)
*
* methods that are written as string objects for ease of use
* Save him as a. js file, to facilitate the extension of the function of the string object
*
* Method Name function
* ----------- --------------------------------
* Trim Remove First space
* Occurs statistics the number of occurrences of the specified character
* Whether the IsDigit check is made up of numbers
* Isalpha Check whether it consists of a number of letters and underscores
* Isnumber Check whether the number
* LenB Returns the number of bytes
* Isinchinese Check whether Chinese characters are included
* Isemail Simple Email check
* IsDate a simple date check to successfully return the Date object
* Isinlist Check if there are characters in the list
* Isinlist Check if there are characters in the list
*/
/*** Remove the trailing space ***/
String.prototype.Trim = function () {
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}
/*** count the number of occurrences of the specified character ***/
String.prototype.Occurs = function (ch) {
var re = eval ("/[^" +ch+ "]/g");
Return This.replace (Re, ""). Length;
return This.split (CH). Length-1;
}
/*** Check if it's made up of numbers ***/
String.prototype.isDigit = function () {
var s = this. Trim ();
Return (S.replace (/\d/g, ""). Length = = 0);
}
/*** Check to see if the numbers are composed of alphanumeric letters and underscores ***/
String.prototype.isAlpha = function () {
Return (This.replace (/\w/g, ""). Length = = 0);
}
/*** Check for a number of ***/
String.prototype.isNumber = function () {
var s = this. Trim ();
Return (S.search (/^[+-]?[ 0-9.]*$/) >= 0);
}
/*** returns the number of bytes ***/
String.prototype.lenb = function () {
Return This.replace (/[^\x00-\xff]/g, "* *"). Length;
}
/*** Check to see if Chinese characters are included ***/
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.exec (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);
}
/*** a simple date check to successfully return a Date object ***/
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 to see if there are any character characters in the list ***/
String.prototype.isInList = function (list) {
var re = eval ("/[" +list+ "]/");
Return Re.test (this);
}

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.