jsp页面上一些js校验的方法

客户端校验!


//一些js校验的方法。
//函数返回true,则数值正确,返回false则数值格式错误
//基本参数说明:value:值,name:控件名称,需要提示的名称,len:控件长度,isNull:是否为空,true可以为空

//基本校验-非空校验
function valNull(value, name) {
if (value == "") {
alert(name + "值不能为空!");
return false;
} else
return true;
}

//基本校验-特殊字符校验
function valSpecail(value, name) {
var exp = /[\!\@\#\$\%\^\&\*~<>',\.]/;
if (exp.test(value)) {
alert(name + "值含有特殊字符或空格!");
return false;
} else
return true;
}

//基本校验-长度验证
function valLen(value, name, len) {
if (value.length > len) {
alert(name + "值长度超过最大" + len + "位!");
return false;
} else
return true;
}

//基本校验 数字
function valNum(value, name) {
var ext = /^[0-9]*$/;
if (!ext.test(value)) {
alert(name + "格式不正确!");
return false;
} else
return true;
}

//基本校验 小数
function valDou(value, name) {
var ext = /^[0-9]+(.[0-9]{1,3})?$/;
if (!ext.test(value)) {
alert(name + "格式不正确!");
return false;
} else
return true;
}

//基本验证 电话 - 座机
function valTel(value, name) {
//座机
var exp1 = /^\d{3,4}-\d{7,8}(-\d{3,5})*$/;
if (exp1.test(value))
return true;
alert(name + "格式不正确!");
return false;
}

//基本校验 手机
function valMob(value, name) {
//手机
var exp2 = /^\d{11}$/;
if (exp2.test(value))
return true;
alert(name + "格式不正确!");
return false;
}

//邮编
function postCodeVal(value, name, isNull) {
//是否为空
if (isNull && value.length == 0)
return true;
//非空
if (!valNull(value, name))
return false;

var exp = /^\d{6}$/;
if (!exp.test(value)) {
alert("邮编格式不正确!");
return false;
} else
return true;
}

//字符串校验
function strValSp(value, name, len, isNull) {
//特殊字符
if (!valSpecail(value, name))
return false;
if (!strVal(value, name, len, isNull))
return false;
return true;
}

//字符串校验 不含特殊字符检测
function strVal(value, name, len, isNull) {
//是否为空
if (isNull && value.length == 0)
return true;
//非空
if (!valNull(value, name))
return false;
//长度
if (!valLen(value, name, len))
return false;
return true;
}

//邮件验证
//isNull是否可以为空:true是
function mailVal(value, name, len, isNull) {
//是否为空
if (isNull && value.length == 0)
return true;
//非空
if (!valNull(value, name))
return false;
//长度
if (!valLen(value, name, len))
return false;

//邮件格式
var exp = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
if (!exp.test(value)) {
alert(name + "格式错误!");
return false;
}

return true;
}

//数字校验:type:0正整数,1正小数
function numberVal(value, name, len, isNull, type) {
//是否为空
if (isNull && value.length == 0)
return true;

//验证非空
if (!valNull(value, name))
return false;

//长度
if (!valLen(value, name, len))
return false;

//格式
if (type == 0)
return valNum(value, name);
else
return valDou(value, name);
}

//电话校验:type:0座机,1手机
function telVal(value, name, isNull, type) {
//是否为空
if (isNull && value.length == 0)
return true;

//验证非空
if (!valNull(value, name))
return false;

if (type == 0)
return valTel(value, name);
else if (type == 1)
return valMob(value, name);
else
return false;

}

//判断是否是中文
function isChinese(temp) {
var re = /[^\u4e00-\u9fa5]/;
return re.test(temp);
}

//输入时数字判断
function isNumber() {
if (event.keyCode <= 57 && event.keyCode >= 48)
return true;
else
return false;
}

//验证含有汉字的字符串长度(一个汉字为2个字节)
function checkLength(val, info, length, isChinese) {
//特殊字符
if (!valSpecail(val, info))
return false;
var count = val.length;
var num = 0;
if (isChinese) {
num = length / 2;
} else {
num = length;
}
var Expression = /^[\u0391-\uFFE5]+$/;
var objExp = new RegExp(Expression);
if (count > num) {
alert(info + "长度超过最大" + num + "位!");
return false;
} else {
return true;
}
}
//验证输入的内容中是否含有汉字
function checkChinese(val, info, length, isChinese, isNull) {
if (!isNull) {
if (val == '') {
alert(info + "不允许为空!");
return false
}
}
//特殊字符
if (!valSpecail(val, info))
return false;
//在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号
var Expression = /^[\u0391-\uFFE5]+$/;
var objExp = new RegExp(Expression);
//如果允许有汉字
if (isChinese) {
//验证含有汉字的字符串长度(一个汉字为2个字节)
return checkLength(val, info, length, isChinese);
} else {
//如果不允许有汉字
for ( var i = 0; i < val.length; i++) {
if (objExp.test(val.charAt(i)) == true) {
alert(info + "不允许有中文!");
return false;
}
}
return checkLength(val, info, length, isChinese);
}
}


以上是一个js文件pageValidate.js里面所有代码,包含常见的一些校验函数

下面是一个jsp页面上校验的总的函数示例:
	function addShift1(){

if(strValSp($("#platerNumber").val(), "车牌号码", 16, false)
&& strValSp($("#type").val(), "货车类型", 32, false)
&& valDou($("#width").val(), "车宽", false)
&& valDou($("#heigth").val(), "车高", false)
&& valDou($("#length").val(), "车长", false)
&& valDou($("#canCarryTon").val(), "核定载质量", false)
&& valDou($("#selfWeight").val(), "车辆标记总质量", false)
&& valDou($("#axleNumber").val(), "轴数", false)
//&& valDou($("#suoshuType").val(), "运输方类型", false)
//&& valDou($("#ownerName").val(), "运输方名称", false)
){

jQuery("#orgForm").submit();
}

}



下面是jsp页面表单(id为orgForm)里面对上面这个函数的调用:
					<tr>
<td colspan="2" class=" txtCenter" ><a href="javascript:addShift1();" ><img src="img/ico_12.gif" width="76" height="22" /></a> </td>
</tr>

ico_12.gif是一个保存的图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值