Some JavaScript detection functions that are often used

Source: Internet
Author: User
Tags date format functions getdate integer rtrim trim valid
javascript| functions//function Name:trim
Function Description: Removing the trailing spaces of a string
Creation Date:2004-7-13 15:30
Last Modify by:n/a
Last Modify date:n/a
String.prototype.trim=function () {
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}

Function Name:ltrim
Function Description: Remove space on the left side of a string
Creation Date:2004-7-13 9:58
Last Modify by:n/a
Last Modify date:n/a
String.prototype.ltrim=function ()
{
Return This.replace (/(^\s*)/g, "");
}

Function Name:rtrim
Function Description: Remove the space on the right side of the string
Creation Date:2004-7-13 15:31
Last Modify by:n/a
Last Modify date:n/a
String.prototype.rtrim=function ()
{
Return This.replace (/(\s*$)/g, "");
}

Function Name:len
Function Description: Returns the actual length of the string, one Chinese character counts 2 lengths
Creation Date:2004-7-13 9:58
Last Modify by:n/a
Last Modify date:n/a
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:2004-7-13 9:58
Last Modify by:n/a
Last Modify date:n/a
String.prototype.isvaliddate=function ()
{
var result=this.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/);
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 a valid time format-"HH:MM:SS"
Creation Date:2004-7-13 9:58
Last Modify by:n/a
Last Modify date:n/a
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 e-mail message
Creation Date:2004-7-13 9:59
Last Modify by:n/a
Last Modify date:n/a
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:2004-7-13 9:59
Last Modify by:n/a
Last Modify date:n/a
String.prototype.isvaliddatetime=function ()
{
var result=this.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\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:2004-7-13 10:01
Last Modify by:n/a
Last Modify date:n/a
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:2004-7-13 10:01
Last Modify by:n/a
Last Modify date:n/a
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:2004-7-13 10:28
Last Modify by:n/a
Last Modify date:n/a
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:2004-7-13 10:01
Last Modify by:n/a
Last Modify date:n/a
String.prototype.isvalidnumber=function ()
{
Return!isnan (this);
}

Function name:isvalidletters
Function Description: Determines whether the input is a A-z/A-Z string
Creation Date:2004-7-13 10:10
Last Modify by:n/a
Last Modify date:n/a
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:2004-7-13 10:10
Last Modify by:n/a
Last Modify date:n/a
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 composed of 0-9/A-z/A-Z
Creation Date:2004-7-13 10:14
Last Modify by:n/a
Last Modify date:n/a
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 by 0-9/A-z/A-z/. /_ A string consisting of
Creation Date:2004-7-13 10:20
Last Modify by:n/a
Last Modify date:n/a
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:2004-7-13 10:22
Last Modify by:n/a
Last Modify date:n/a
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:2004-7-13 10:22
Last Modify by:n/a
Last Modify date:n/a
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 cell phone number
Creation Date:2004-7-13 10:23
Last Modify by:n/a
Last Modify date:n/a
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.