常用的JS驗(yàn)證和函數(shù)匯總
來源:易賢網(wǎng) 閱讀:735 次 日期:2014-12-26 15:34:09
溫馨提示:易賢網(wǎng)小編為您整理了“常用的JS驗(yàn)證和函數(shù)匯總”,方便廣大網(wǎng)友查閱!

下面是我常用一些JS驗(yàn)證和函數(shù),有一些驗(yàn)證我直接寫到了對(duì)象的屬性里面了,可以直接通過對(duì)象.方法來調(diào)用

代碼如下:

//浮點(diǎn)數(shù)除法運(yùn)算

function fdiv(a, b, n) {

if (n == undefined) { n = 2; }

var t1 = 0, t2 = 0, r1, r2;

try { t1 = a.toString().split(".")[1].length } catch (e) { }

try { t2 = b.toString().split(".")[1].length } catch (e) { }

with (Math) {

r1 = Number(a.toString().replace(".", ""));

r2 = Number(b.toString().replace(".", ""));

return ((r1 / r2) * pow(10, t2 - t1)).toFixed(n);

}

}

代碼如下:

//浮點(diǎn)數(shù)乘法運(yùn)算

function fmul(a, b, n) {

if (n == undefined) { n = 2; }

var m = 0, s1 = a.toString(), s2 = b.toString();

try { m += s1.split(".")[1].length } catch (e) { }

try { m += s2.split(".")[1].length } catch (e) { }

return (Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)).toFixed(n);

}

代碼如下:

//浮點(diǎn)數(shù)加法運(yùn)算

function fadd(a, b, n) {

if (n == undefined) { n = 2; }

var r1, r2, m;

try { r1 = a.toString().split(".")[1].length } catch (e) { r1 = 0 }

try { r2 = b.toString().split(".")[1].length } catch (e) { r2 = 0 }

m = Math.pow(10, Math.max(r1, r2))

return ((a * m + b * m) / m).toFixed(n);

}

代碼如下:

//浮點(diǎn)數(shù)減法運(yùn)算

function fsub(a, b, n) {

if (n == undefined) { n = 2; }

var r1, r2, m;

try { r1 = a.toString().split(".")[1].length } catch (e) { r1 = 0 }

try { r2 = b.toString().split(".")[1].length } catch (e) { r2 = 0 }

m = Math.pow(10, Math.max(r1, r2));

//動(dòng)態(tài)控制精度長(zhǎng)度

//n = (r1 >= r2) ? r1 : r2;

return ((a * m - b * m) / m).toFixed(n);

}

Number.prototype.add = function (arg) {

return fadd(this, arg);

}

Number.prototype.subs = function (arg) {

return fsub(this, arg);

}

Number.prototype.mul = function (arg) {

return fmul(this, arg);

}

Number.prototype.div = function (arg) {

return fdiv(this, arg);

}

代碼如下:

///格式化數(shù)字位數(shù),不足位數(shù)默認(rèn)左邊補(bǔ)0,如果指定了參數(shù)2并且參數(shù)2的值為1則右邊補(bǔ)0

Number.prototype.FormatLen = function (len, direct) {

var d = parseInt(direct);

if (isNaN(d)) { d = 0; }

var num = this.toString();

if (num.length < len) {

for (var i = num.length; i < len; i++) {

if (d == 0) {

num = "0" + num;

}

else {

num += "0";

}

}

}

return num;

}

代碼如下:

//格式化小數(shù)點(diǎn)數(shù)位,可以指定小數(shù)位數(shù),是否四舍五入等參數(shù)

Number.prototype.FormatRadix = function (len, IsRound) {

var num = this.toString();

var numArr = num.split('.');

var rad = 0;

var numpart = parseInt(numArr[0]);

if (numArr.length >= 2) {

if (numArr[1].length < len) {

rad = parseInt(numArr[1]).FormatLen(len, 1);

}

else {

if (numArr[1].length == len) {

rad = numArr[1];

}

else {

rad = numArr[1].substr(0, len);

if (IsRound) {

var d = parseInt(numArr[1].substr(len, 1));

if (d >= 5) { rad += 1; if (rad.toString().length > len) { numpart += 1; rad = rad.toString().substr(1, len); } }

}

}

}

}

else {

rad = rad.FormatLen(len);

}

return numpart + "." + rad;

}

代碼如下:

//檢測(cè)字符串中是否有相同的元素split是字符串分隔符,如果指定了分隔符,則判斷分隔符為分隔的字符串是否有重復(fù),如果沒指定則判斷單個(gè)字符串是否有重復(fù)

//有重復(fù)返回true

String.prototype.CompareElement = function (s) {

var str = this.toString();

if (s == undefined) {

for (var i = 0; i < str.length; i++) {

for (j = i + 1; j < str.length; j++) {

if (str.substr(i, 1) == str.substr(j, 1)) {

return true;

}

}

}

}

else {

var strArr = str.split(s);

for (var i = 0; i < strArr.length; i++) {

for (var j = i + 1; j < strArr.length; j++) {

if (strArr[i] == strArr[j]) {

return true;

}

}

}

}

return false;

}

String.prototype.replaceAll = function (str, tostr) {

oStr = this;

while (oStr.indexOf(str) > -1) {

oStr = oStr.replace(str, tostr);

}

return oStr;

}

Array.prototype.CompareElement = function () {

var strArr = this;

for (var i = 0; i < strArr.length; i++) {

for (var j = i + 1; j < strArr.length; j++) {

if (strArr[i] == strArr[j]) {

return true;

}

}

}

return false;

}

代碼如下:

//字符串轉(zhuǎn)組數(shù),如果未指定分隔符s,則默認(rèn)以,分隔分隔符,如果指定分隔符為空則將每個(gè)字符作為一個(gè)數(shù)組元素

String.prototype.ToArray = function (s) {

if (s == undefined) { s = ","; }

var strArr = [];

strArr = this.split(s);

return strArr;

}

代碼如下:

//將一個(gè)數(shù)組轉(zhuǎn)換一個(gè)字符串,所有元素使用指定的分隔符連接,默認(rèn)分隔為,

Array.prototype.ToIDList = function (s) {

if (s == undefined) { s = ","; }

var list = "";

for (var i = 0; i < this.length; i++) {

list += (list == "" ? this[i] : s + "" + this[i]);

}

return list;

}

代碼如下:

//獲取指定元素的位置索引,元素不存在返回-1

Array.prototype.GetIndex = function (s) {

var index = -1;

for (var i = 0; i < this.length; i++) {

if ((s + "") == this[i]) {

index = i;

}

}

return index;

}

代碼如下:

//將指定元素從數(shù)組中刪除

Array.prototype.Remove = function (s) {

var list = "";

for (var i = 0; i < this.length; i++) {

if (s != this[i]) {

list += (list == "" ? this[i] : "," + this[i]);

}

}

return list.ToArray();

}

代碼如下:

///將數(shù)組進(jìn)行數(shù)字排序asc指定是否進(jìn)行升序排序,可以為true或者false,未指定為升序

Array.prototype.SortByNumber = function (asc) {

if (asc == undefined) { asc = true; }

if (asc) {

return this.sort(SortNumberAsc);

}

else {

return this.sort(SortNumberDesc);

}

}

Array.prototype.InArray = function (e) {

var IsIn = false;

for (var i = 0; i < this.length; i++) {

if (this[i] == (e + "")) {

IsIn = true;

}

}

return IsIn;

}

String.prototype.Trim = function (s) { return Trim(this, s); }

String.prototype.LTrim = function (s) { return LTrim(this, s); }

String.prototype.RTrim = function (s) { return RTrim(this, s); }

//配合Array.SortByNumer使用,將數(shù)字進(jìn)行數(shù)組降序排序

function SortNumberDesc(a, b) {

return b - a;

}

//配合Array.SortByNumer使用,將數(shù)字進(jìn)行數(shù)組升序排序

function SortNumberAsc(a, b) {

return a - b;

}

//此處為獨(dú)立函數(shù)

function LTrim(str, s) {

if (s == undefined) { s = " "; }

if (str == s && s != " ") { return s; }

var i;

for (i = 0; i < str.length; i++) {

if (str.charAt(i) != s && str.charAt(i) != s) break;

}

str = str.substring(i, str.length);

return str;

}

function RTrim(str, s) {

var i;

if (str == s && s != " ") { return s; }

if (s == undefined) { s = " "; }

for (i = str.length - 1; i >= 0; i--) {

if (str.charAt(i) != s && str.charAt(i) != s) break;

}

str = str.substring(0, i + 1);

return str;

}

function Trim(str, s) {

return LTrim(RTrim(str, s), s);

}

代碼如下:

///檢測(cè)字符串是否是由中文,英文,數(shù)字以及下劃線組成的

function chkNickName(str) {

var pattern = /^[\w\u4e00-\u9fa5]+$/gi;

if (pattern.test(str)) {

return true;

}

return false;

}

代碼如下:

//判斷長(zhǎng)度(長(zhǎng)度不限為0)

String.prototype.IsLen = function () {

var isRightFormat = false;

var minnum = arguments[0] ? arguments[0] : 0;

var maxnum = arguments[1] ? arguments[1] : 0;

isRightFormat = (minnum == 0 && maxnum == 0 ? true : (calculate_byte(this) >= minnum && calculate_byte(this) <= maxnum ? true : false));

return isRightFormat;

}

代碼如下:

//驗(yàn)證字符串是否為字母+數(shù)字+_+-

String.prototype.IsStr = function () {

var myReg = /^[0-9a-zA-Z\-\_]+$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證用戶名

String.prototype.IsUsername = function () {

var myReg = /^[0-9a-zA-Z\-\_]{3,50}$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證密碼

String.prototype.IsPassword = function () {

var myReg = /^[0-9a-zA-Z`~!@#$%^&*()-_+=\{\}\[\]\;\:\"\'\?\/\\]{6,}$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證是否為字母

String.prototype.IsEn = function () {

var myReg = /^[a-zA-Z]+$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證是否為漢字

String.prototype.IsCn = function () {

var myReg = /^[\u0391-\uFFE5]+$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證E-mail

String.prototype.IsEmail = function () {

var myReg = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證MSN

String.prototype.IsMSN = function () {

var myReg = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證QQ號(hào)碼

String.prototype.IsQQ = function () {

var myReg = /^[1-9]\d{4,10}$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證網(wǎng)址

String.prototype.IsHttpUrl = function () {

var myReg = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證域名

String.prototype.IsDoMainName = function () {

var myReg = /^[0-9a-zA-Z]([0-9a-zA-Z\-]+\.){1,3}[a-zA-Z]{2,4}?$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證IPV4地址

String.prototype.IsIpv4 = function () {

var myReg = /^(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2})$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證圖片地址(不支持由CGI動(dòng)態(tài)生成的圖片)

String.prototype.IsImgURL = function () {

var myReg = /^\.(jpeg|jpg|gif|bmp|png|pcx|tiff|tga|lwf)$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證手機(jī)號(hào)碼

String.prototype.IsCellPhone = function () {

var myReg = /^((\(\d{3}\))|(\d{3}\-))?1[3,5]\d{9}$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證固定電話

String.prototype.IsPhone = function () {

var myReg = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證郵編

String.prototype.IsZipCode = function () {

var myReg = /[0-9]{6}/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證身份證號(hào)碼

String.prototype.IsIdCard = function () {

var myReg = /(^([\d]{15}|[\d]{18}|[\d]{17}[xX]{1})$)/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證日期格式Y(jié)Y-MM-DD

String.prototype.IsDateFormat = function () {

var myReg = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證時(shí)間格式HH:MM:SS

String.prototype.IsRangeTime = function () {

var myReg = /^(\d{2}):(\d{2}):(\d{2})$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//驗(yàn)證金額格式

String.prototype.IsMoney = function () {

var myReg = /^[0-9]{1,8}[.]{0,1}[0-9]{0,6}$/;

if (myReg.test(this)) return true;

return false;

}

代碼如下:

//字驗(yàn)證數(shù)字格式并判斷數(shù)字的圍(min:最小值;max:最大值.)

String.prototype.IsInt = function () {

var isRightFormat = false;

var minnum = arguments[0] ? arguments[0] : 0;

var maxnum = arguments[1] ? arguments[1] : 0;

var myReg = /^[-\+]?\d+$/;

if (myReg.test(this)) {

isRightFormat = (minnum == 0 && maxnum == 0 ? true : (this > minnum && this < maxnum ? true : false));

}

return isRightFormat;

}

代碼如下:

//驗(yàn)證搜索關(guān)鍵字

String.prototype.IsSearch = function () {

var myReg = /^[\|\"\'<>,.*&@#$;:!^()]/;

if (myReg.test(this)) return false;

return true;

}

代碼如下:

//js準(zhǔn)確計(jì)算字符串長(zhǎng)度

function calculate_byte(sTargetStr) {

var sTmpStr, sTmpChar;

var nOriginLen = 0;

var nStrLength = 0;

sTmpStr = new String(sTargetStr);

nOriginLen = sTmpStr.length;

for (var i = 0; i < nOriginLen; i++) {

sTmpChar = sTmpStr.charAt(i);

if (escape(sTmpChar).length > 4) {

nStrLength += 2;

} else if (sTmpChar != '\r') {

nStrLength++;

}

}

return nStrLength;

}

代碼如下:

//顏色值;

String.prototype.IsColor = function () {

var s = arguments[0] ? arguments[0] : "";

s = s.Trim();

if (s.length != 7) return false;

return s.search(/\#[a-fA-F0-9]{6}/) != -1;

}

代碼如下:

//js日期格式化

Date.prototype.format = function (format) {

var o = {

"M+": this.getMonth() + 1, //month

"d+": this.getDate(), //day

"h+": this.getHours(), //hour

"m+": this.getMinutes(), //minute

"s+": this.getSeconds(), //second

"q+": Math.floor((this.getMonth() + 3) / 3), //quarter

"S": this.getMilliseconds() //millisecond

}

if (/(y+)/.test(format)) {

format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

}

for (var k in o) {

if (new RegExp("(" + k + ")").test(format)) {

format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));

}

}

return format;

}

function HasChinese(value) {

if (/^[\u4e00-\u9fa5]+$/.test(value)) {

return true;

}

return false;

}

function ToDate(dateStr) {

var dStr = dateStr.toString();

dateStr = dStr.replaceAll("-", "/");

return new Date(Date.parse(dateStr));

}

代碼如下:

//是否ID列表

String.prototype.IsIdList = function (s) {

if (s == undefined) {

s = ",";

}

var arr = this.split(s);

for (var i = 0; i < arr.length; i++) {

if (isNaN(parseInt(arr[i]))) { return false; }

}

return true;

}

代碼如下:

//獲取事件觸發(fā)的對(duì)象

function getEventTarget(e) {

e = e || window.event;

return e.target || e.srcElement;

}

代碼都很簡(jiǎn)潔,簡(jiǎn)單,功能卻都很實(shí)用,有需要的小伙伴參考下

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)頁制作
易賢網(wǎng)手機(jī)網(wǎng)站地址:常用的JS驗(yàn)證和函數(shù)匯總
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)