Created
August 3, 2010 01:00
-
-
Save huacnlee/505620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 验证是否是一个数值型 | |
* 合法格式: 32位整型值 如1 52 5555 414 | |
*/ | |
String.prototype.isInt32 = function() { | |
var regex = new RegExp("^[\d]{1,32}$"); | |
return regex.exec(this); | |
} | |
/** | |
* 日期验证 | |
* 合法格式:1988-12-1 1982-01-05 1996-5-1 2004/1/2 2005/01/15 | |
*/ | |
String.prototype.isDate = function() { | |
var regex = new RegExp("^[\d]{4}([-|/])[\d]{1,2}([-|/])[\d]{1,2}$"); | |
return regex.exec(this); | |
} | |
/** | |
* 是否是一个合法的SpaceName或UserName | |
* 格式:huacnlee-2 huacnlee_2 | |
*/ | |
String.prototype.isUserName = function() { | |
var regex = new RegExp("^[a-zA-Z0-9\-_]{4,16}$"); | |
return regex.exec(this); | |
} | |
/** | |
* 是否是合法的Email | |
*/ | |
String.prototype.isEmail = function() { | |
var regex = new RegExp("^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); | |
return regex.exec(this); | |
} | |
/** | |
* 是否是一个合法的URL地址 | |
*/ | |
String.prototype.isUrl = function() { | |
var regex = new RegExp("^http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$"); | |
return regex.exec(this); | |
} | |
/** | |
* 当最后个字符和参数一相同时,删除最后个字符 | |
*/ | |
String.prototype.removeLastChar = function(c) { | |
if (this.substr(this.length - 1, 1) == c) { | |
return this.substr(0, this.length - 1); | |
} | |
else { | |
return this; | |
} | |
} | |
/** | |
* 清除首尾的空格 | |
*/ | |
String.prototype.trim = function() { | |
return this.replace(/^\s+|\s+$/g, ""); | |
} | |
/** | |
* 清除右边的空格 | |
*/ | |
String.prototype.trimRight = function() { | |
return this.replace(/\s+$/, ""); | |
} | |
/** | |
* 清除左边空格 | |
*/ | |
String.prototype.trimLeft = function() { | |
return this.replace(/^\s+/, ""); | |
} | |
/** | |
* 替换所有,自带的Replace只能替换一个 | |
* @param {String} from 原字符串,正则条件 | |
* @param {String} to 新字符串 | |
*/ | |
String.prototype.replaceAll = function(from, to) { | |
var regex = new RegExp(from); | |
return this.replace(regex, to); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment