一:JQuery驗(yàn)證表單:
if($("#insertDABox").attr("checked")){ //如果選擇插入編號(hào)
$("#saveType").attr("value","insert");
}else{
$("#saveType").attr("value","add");
}
}
//校驗(yàn)表單
function checkDept(){
if($("#deptAcct").val()=="" || null==$("#deptAcct").val() ){
alert("請(qǐng)輸入單位編號(hào)");
$("#deptAcct").focus();
return false;
}
if($("#deptAcct").val().length>4){
alert("請(qǐng)輸入單位編號(hào)太長(zhǎng)");
$("#deptAcct").focus();
return false;
}
if($("#deptName").val()=="" || null==$("#deptName").val() ){
alert("請(qǐng)輸入單位名稱");
$("#deptName").focus();
return false;
}
if($("#deptName").val().length>25){
alert("輸入的單位名稱太長(zhǎng)");
$("#deptName").focus();
return false;
}
var regEmail=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
var rege = new RegExp(regEmail);
if($("#deptEmail").val().length>0){
if(!rege.test($("#deptEmail").val())){
alert("輸入的郵件不正確!");
$("#deptEmail").focus();
return false;
}
}
if($("#deptTel").val().length>13){
alert("輸入電話號(hào)碼不正確!");
$("#deptTel").focus();
return false;
}
2、使用javascript封裝對(duì)象:
(1)、封裝類:
function validate_required(field,alerttxt){
with (field) {
if (value==null||value=="")
{alert(alerttxt);return false}
else {return true}
}
}
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2){
alert(alerttxt);return false}
else {return true}
}
}
(2)、前臺(tái)調(diào)用:
function validate_form(thisform){
with (thisform){
if (validate_required(issueName,"雜志名不能為空!")==false){
issueName.focus();return false
}
if (validate_required(nextIssue,"下一期刊號(hào)不能為空!")==false){
nextIssue.focus();return false
}
}
return true;
}
3、使用Ajax表單驗(yàn)證:
(1)、jsp頁(yè)面
$.post('${path}/deptServlet?method=insert¤tPage=${currentPage}',{
deptAcct:$("#deptAcct").val(),
deptName:$("#deptName").val()
},function(data){
if('0'==data){
alert("請(qǐng)單位編號(hào)錯(cuò)誤,請(qǐng)重新輸入!");
$("#deptAcct").focus();
return false;
}else{
alert("添加成功");
window.location.href="${path}/deptServlet?method=goToInsert";
}
///pages/rights/admin/dept/insert.jsp
},'josn');
(2)、java:
sendJson(“你輸入的信息不正確!!”, resp);