一:JQuery驗證表單:
if($("#insertDABox").attr("checked")){ //如果選擇插入編號
$("#saveType").attr("value","insert");
}else{
$("#saveType").attr("value","add");
}
}
//校驗表單
function checkDept(){
if($("#deptAcct").val()=="" || null==$("#deptAcct").val() ){
alert("請輸入單位編號");
$("#deptAcct").focus();
return false;
}
if($("#deptAcct").val().length>4){
alert("請輸入單位編號太長");
$("#deptAcct").focus();
return false;
}
if($("#deptName").val()=="" || null==$("#deptName").val() ){
alert("請輸入單位名稱");
$("#deptName").focus();
return false;
}
if($("#deptName").val().length>25){
alert("輸入的單位名稱太長");
$("#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("輸入電話號碼不正確!");
$("#deptTel").focus();
return false;
}
2、使用javascript封裝對象:
(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)、前臺調用:
function validate_form(thisform){
with (thisform){
if (validate_required(issueName,"雜志名不能為空!")==false){
issueName.focus();return false
}
if (validate_required(nextIssue,"下一期刊號不能為空!")==false){
nextIssue.focus();return false
}
}
return true;
}
3、使用Ajax表單驗證:
(1)、jsp頁面
$.post('${path}/deptServlet?method=insert¤tPage=${currentPage}',{
deptAcct:$("#deptAcct").val(),
deptName:$("#deptName").val()
},function(data){
if('0'==data){
alert("請單位編號錯誤,請重新輸入!");
$("#deptAcct").focus();
return false;
}else{
alert("添加成功");
window.location.href="${path}/deptServlet?method=goToInsert";
}
///pages/rights/admin/dept/insert.jsp
},'josn');
(2)、java:
sendJson(“你輸入的信息不正確!!”, resp);