var userInfo = ValidateHelper.initInfo();
if (userInfo == null) {
return;
}
$.ajax({
type: "post",
dataType: "text",
url: "Ajax/Module.aspx",
timeout: 30000,
data: { UserName: userInfo.UserName, UserPwd: userInfo.UserPwd },
success: function(data) {
alert(data.toString());
}
});
}
这边呢要是在提交数据的时候会调用validateStringValueIsEmpty方法,要是返回的是失败 是不会真的提交给服务器端得。
那上面的情况是一个最简单的处理不为空的情况,要是我们想验证是否是Email 是否是身份证号码,这些复杂的验证实现如下:
复制代码 代码如下:
var Validation = {
textCount: function(field, counter, maxLimit) {
var message = $(field).val();
if ($(field).val().length > maxLimit)
$(field).val(message.substring(0, maxLimit))
//$(counter).text(maxLimit-field.value.length);
},
refreshValidator: function(img, input) {
$(img).attr('src', $(img).attr('src') + "&r=" + Math.random());
$(input).focus();
},
isUrl: function(s) {
var strRegex =
/^((http(s)?|ftp|telnet|news|rtsp|mms):\/\/)?(((\w(\-*\w)*\.)+[a-zA-Z]{2,4})|(((1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).){3}(1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).?))(:\d{0,5})?(\/+.*)*$/;
return strRegex.test(s);
},
isDecimal: function(d) { var pattern = /^(([1-9]\d{0,12})|0)(\.\d{1,2})?$/; return pattern.test(d); },
isEmail: function(s) {
var pattern = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
return pattern.test(s);
},
isLowEmail: function(s) {
var b, e;
b = s.indexOf("@");
e = s.indexOf(".");
if (b <= 0) return false;
if (e < 0 || e == (s.length - 1)) { return false; }
return true;
},
clearNoNum: function(event, obj) {
event = window.event || event;
if (event.keyCode == 37 | event.keyCode == 39) {
return;
}
obj.value = obj.value.replace(/[^\d.]/g, "");
obj.value = obj.value.replace(/^\./g, "");
obj.value = obj.value.replace(/\.{2,}/g, ".");
obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
},
checkNum: function(obj) {
obj.value = obj.value.replace(/\.$/g, "");
},
isInteger: function(value) {
var integerReg = new RegExp(/^\d+$/);
return integerReg.test(value);
},
isValidateReg: function(value) {
var validateReg = /^([A-Za-z0-9\s\-\_\~\!\@\#\$\%\^\&\*\(\)\|\<\>\?\:\;\"\'\.\[\]\{\}\,\+\`\/\\\=]){6,16}$/;
if (validateReg.test(value)) {
return true;
}
return false;
},
isDate: function(strValue) {
var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/