Some frequently used Javascript detection function _ javascript skills

Source: Internet
Author: User
Tags rtrim
Some common Javascript detection functions are extended over prototype. // Function Name: trim
// Function Description: removes spaces at the beginning and end of a string.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. trim = function (){
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}



// Function Name: ltrim
// Function Description: removes spaces on the left of the string.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. ltrim = function ()
{
Return this. replace (/(^ \ s *)/g ,"");
}

// Function Name: rtrim
// Function Description: removes spaces on the right of the string.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. rtrim = function ()
{
Return this. replace (/(\ s * $)/g ,"");
}

// Function Name: len
// Function Description: returns the actual length of the string. One Chinese character is counted as two lengths.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. len = function ()
{
Var str = this;
Return str. replace (/[^ \ x00-\ xff]/g, "**"). length
}

// Function Name: isValidDate
// Function Description: determines whether the input is a valid short date format-"YYYY-MM-DD"
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidDate = function ()
{
Var result = this. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d {}) $ /);
If (result = null) return false;
Var d = new Date (result [1], result [3]-1, result [4]);
Return (d. getFullYear () = result [1] & d. getMonth () + 1 = result [3] & d. getDate () = result [4]);
}

// Function Name: isValidTime
// Function Description: determines whether the input is in a valid time format-"HH: MM: SS"
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidTime = function ()
{
Var resule = this. match (/^ (\ d {1, 2 })(:)? (\ D {1, 2}) \ 2 (\ d {1, 2}) $ /);
If (result = null) return false;
If (result [1]> 24 | result [3]> 60 | result [4]> 60) return false;
Return true;
}

// Function Name: isValidEmail
// Function Description: determines whether the input is a valid email.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidEmail = function ()
{
Var result = this. match (/^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ /);
If (result = null) return false;
Return true;
}

// Function Name: isValidDatetime
// Function Description: determines whether the input is a valid long date format-"YYYY-MM-DD HH: MM: SS"
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidDatetime = function ()
{
Var result = this. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d }) :( \ d {1, 2}) :( \ d {1, 2}) $ /);
If (result = null) return false;
Var d = new Date (result [1], result [3]-1, result [4], result [5], result [6], result [7]);
Return (d. getFullYear () = result [1] & (d. getMonth () + 1) = result [3] & d. getDate () = result [4] & d. getHours () = result [5] & d. getMinutes () = result [6] & d. getSeconds () = result [7]);
}

// Function Name: isValidInteger
// Function Description: determines whether the input is an integer.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidInteger = function ()
{
Var result = this. match (/^ (-| \ + )? \ D + $ /);
If (result = null) return false;
Return true;
}

// Function Name: isValidPositiveInteger
// Function Description: determines whether the input is a positive integer.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidPositiveInteger = function ()
{
Var result = this. match (/^ \ d + $ /);
If (result = null) return false;
If (parseInt (this)> 0) return true;
Return false;
}

// Function Name: isValidNegativeInteger
// Function Description: determines whether the input is a negative integer.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidNegativeInteger = function ()
{
Var result = this. match (/^-\ d + $ /);
If (result = null) return false;
Return true;
}

// Function Name: isValidNumber
// Function Description: determines whether the input is a number.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidNumber = function ()
{
Return! IsNaN (this );
}

// Function Name: isValidLetters
// Function Description: determines whether the input is a string consisting of A-Z/a-z
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidLetters = function ()
{
Var result = this. match (/^ [a-zA-Z] + $ /);
If (result = null) return false;
Return true;
}

// Function Name: isValidDigits
// Function Description: determines whether the input is a number consisting of 0-9.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidDigits = function ()
{
Var result = this. match (/^ [1-9] [0-9] + $ /);
If (result = null) return false;
Return true;
}

// Function Name: isValidAlphanumeric
// Function Description: determines whether the input is a string consisting of 0-9/A-Z/a-z
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidAlphanumeric = function ()
{
Var result = this. match (/^ [a-zA-Z0-9] + $ /);
If (result = null) return false;
Return true;
}

// Function Name: isValidString
// Function Description: determines whether the input is a string consisting of 0-9/A-Z/a-z /./_
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidString = function ()
{
Var result = this. match (/^ [a-zA-Z0-9 \ s. \-_] + $ /);
If (result = null) return false;
Return true;
}

// Function Name: isValidPostalcode
// Function Description: determines whether the input is a valid zip code.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidPostalcode = function ()
{
Var result = this. match (/(^ [0-9] {6} $ )/);
If (result = null) return false;
Return true;
}

// Function Name: isValidPhoneNo
// Function Description: determines whether the input is a valid phone number.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidPhoneNo = function ()
{
Var result = this. match (/(^ [0-9] {3, 4} \-[0-9] {3, 8} $) | (^ [0-9] {3, 8} $) | (^ \ ([0-9] {3, 4} \) [0-9] {3, 8} $ )/);
If (result = null) return false;
Return true;
}

// Function Name: isValidMobileNo
// Function Description: determines whether the input is a valid mobile phone number.
// Creation Date:
// Last Modify By: N/
// Last Modify Date: N/
String. prototype. isValidMobileNo = function ()
{
Var result = this. match (/(^ 0 {0, 1} 13 [0-9] {9} $ )/);
If (result = null) return false;
Return true;
}
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.