javascript 易忘备留
1.jQuery.validate验证:
- <input type="file" id="avatar" name="avatar" class="{validate:{required:true,accept:true}}" />
- accept接受的是:image (png, jpg, jpeg, gif)
- <input type="file" id="cv" name="cv" class="{validate:{required:true,accept:'docx?|txt|pdf'}}" />
- accept接受的是:document (doc, docx, txt, pdf)
a.rule.max:function(){}
rules: {
TakeMoney: {
required: true,
digits: true,
max: function (value) {
var num = parseInt($("#balance").val());
if (isNaN(num)) return 9999999;
else return num;
}
},
},
b.异步验证:异步验证的页面只能返回”true”或”false”[字符串](没试过bool和int类型的)
UserName: {
required: true,
userNameCheck: true,
remote: {
url: "/BPAjax/CheckUserNameIsExists",
type: "post"
}
},
c.自定义验证
// 中文字两个字节
jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
var length = value.length;
for(var i = 0; i < value.length; i++){
if(value.charCodeAt(i) > 127){
length++;
}
}
return this.optional(element) || ( length >= param[0] && length <= param[1] );
}, $.validator.format("请确保输入的值在{0}-{1}个字节之间(一个中文字算2个字节)"));
// 邮政编码验证
jQuery.validator.addMethod("isZipCode", function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码");
2.原型扩展
a. //格式化日期
Date.prototype.format = function (format) {
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
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 getCurrentDate() {
var date = new Date();
return date.format("yyyy-MM-dd");
}
b.字符串格式化
// 字符串格式化 调用方式同C#
String.prototype.format = function () { var args = arguments; return this.replace(/\{(\d+)\}/g, function (m, i) { return args[i]; }); }
c.字符串截取、长度、移除所有空格(字节长度,双字节占2个)
//首尾空格截取 String.prototype.trim = function() {return this.replace(/(^\s*)|(\s*$)/g, "");} //获取字节长度 String.prototype.len = function() { return this.replace(/[^\x00-\xff]/g, "aa").length; } //去除字符串中所有空格 function removeAllSpace(str) { return str.replace(/\s+/g, "");}
3.其他
a.//获取URL传递参数
Request = { QueryString: function (item) { var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i")); return svalue ? svalue[1] : svalue; } }
b.检测类型
var str = 'str'; var arr = new Array(); var num = 0; var bool = true; var obj = {}; var re = /^[0-9]/; getType(re); // 'regexp' getType(str); // 'string' getType(arr); // 'array' getType(num); // 'number' getType(bool); // 'boolean' getType(obj); // 'object' getType(window.aa); // 'undefined' getType(undefined); // 'undefined' getType(null); // 'null' function getType (o) { /* 检测数据类型函数 */ return Object.prototype.toString.call(o).slice(8,-1).toLowerCase(); }