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#
View Code
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(); }