JS determines numbers, letters, and Chinese characters

Source: Internet
Author: User
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>
<Form name = "form" Action = "" onsubmit = "Return check ();">
<Input type = text name = STR>
<Input type = submit>
<Form>

---------------------------------------------------
/**
* 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 );
}

/// <Summary>
/// Only numbers are allowed
/// </Summary>
/// <Returns> if it is a number or letter, true is returned; otherwise, false is returned. </returns>
String. Prototype. isdigit = function ()
{
VaR Reg =/^ \ D + $/g;
Return Reg. Test (this );
}

/// <Summary>
/// Only numbers and letters are allowed
/// </Summary>
/// <Returns> if it is a number or letter, true is returned; otherwise, false is returned. </returns>
String. Prototype. isalphadigit = function ()
{
VaR Reg =/^ [a-zA-Z0-9] + $/g;
Return Reg. Test (this );
}

/// <Summary>
/// Only numbers, letters, and underscores are allowed
/// </Summary>
/// <Returns> if it is a number, letter, or underline, true is returned; otherwise, false is returned. </returns>
String. Prototype. isalpha = function ()
{
VaR Reg =/^ \ W + $/g;
Return Reg. 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.