//用户名校验的方法
//这个方法将使用XMLHTTPRequest对象来进行AJAX的异步数据交互
var xmlhttp;
function verify() {
    var cdnid = document.getElementById("CDNID").value;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType("text/xml");
        }
    } else if (window.ActiveXObject) {
        var activexName = ["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
        for (var i = 0; i < activexName.length; i++) {
            try{
                xmlhttp = new ActiveXObject(activexName[i]);
                break;
            } catch(e){
            }
        }
    }
    if (!xmlhttp) {
        alert("XMLHttpRequest对象创建失败!!");
        return;
    } else {
        //alert(xmlhttp.readyState);
    }
    
    xmlhttp.onreadystatechange = callback;
    xmlhttp.open("GET","nodevalidate.do?cdnid="+ cdnid,true);
    xmlhttp.send(null);
}
function callback() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var responseText = xmlhttp.responseText;
            var divNode = document.getElementById("result");
            divNode.innerHTML = responseText;
        } else {
            alert("出错了!!!");
        }
    }
}<form class="submitForm" id="submitForm" action="<%=path%>/hosts.do?command=add" method="post">
<table width="362" height="104" border="0" >
  <tr>
    <td width="91" height="23" class="alt" >CDNID:</td>
    <td width="261"  style="border-top:1px solid #C1DAD7;"><input type="text" name="CDNID" /></td>
  </tr>
  <tr>
    <td height="23"  class="alt">CNAME:</td>
    <td><input type="text" name="CNAME" /></td>
  </tr>
  <tr>
   <td class="alt" >&nbsp;</td>
    <td><input type="reset" value="重置" class="a1" />&nbsp;<input type="submit" value="添加" class="a1" ></td>
  </tr>
  
</table></form>后台的servlet
 httpServletResponse.setContentType("text/html;charset=utf-8");
            PrintWriter out = httpServletResponse.getWriter();
            String id = httpServletRequest.getParameter("cdnid");
           // String name = URLDecoder.decode(id,"UTF-8");
            System.out.println(id);
            if(id == null || id.length() == 0){
                out.println("用户名不能为空");
            } else {
            boolean b = queryById(id);
                if(b){
                  out.println("CDNID已经存在!");
                } else {
                    out.println("");
                   
                }
            }
        } catch(Exception e){
            e.printStackTrace();
        }
现在的情况是怎么让他在用户名已经存在的同时,不让form表单提交?
现在form表单随时都可以提交
这样有问题啊

解决方案 »

  1.   

    给form加个这个onsubmiot="return fun()"
    fun()是你ajax提交的那个方法,在ajax请求回来的时候判断条件来进对form的return
    return true;//提交
    return false;//不提交
      

  2.   

    <form class="submitForm" id="submitForm" action="<%=path%>/hosts.do?command=add" method="post" onsubmit="return verify()">然后function callback() {
      if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) {
      var responseText = xmlhttp.responseText;
      var divNode = document.getElementById("result");
      divNode.innerHTML = responseText;
      if(responseText == "x")return false;//如果x表示的是用户已经存在  } else {
      alert("出错了!!!");
      }
      }
    }
      

  3.   

    我加了jquery vaildate 的验证form表单里加了这个 id="submitForm" class="submitForm"为什么表单提交不了?<form id="submitForm" class="submitForm" action="<%=path%>/nodeinfo.do?command=add" method="post" ><table width="362" height="104" border="0" >
      <tr>
        <td width="91" height="23" class="alt" >CDNID:</td>
        <td width="261"  style="border-top:1px solid #C1DAD7;">
        <input type="text" name="CDNID" id="CDNID" onblur="verify()"/><span id="result" style="color:red" ></span></td>
      </tr>
      <tr>
        <td height="23"  class="alt"> CNAME:</td>
        <td><input type="text" name="CNAME" /></td>
      </tr>
      <tr>
        <td height="23"  class="alt">ACTIVE:</td>
        <td>  <!-- <input type="text" name="ACTIVE" value="${node.ACTIVE}" /> -->
    <input type="checkbox" name="ACTIVE" onclick="sele(this);" checked="checked" value="A">活动
        <input type="checkbox" name="ACTIVE" onclick="sele(this);" value="0" >关闭
        </td>
    </tr> </table>
     /**  
      * @author validateclass 
      */  
     $(document).ready(function(){       
             
    /* 设置默认属性 */       
    $.validator.setDefaults({       
        submitHandler: function(form) {    
            form.submit();    
       }       
    });
     // 字符验证
    jQuery.validator.addMethod("stringCheck", function(value, element) { 
        return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);       
    }, "只能包括中文字、英文字母、数字和下划线");
      
    //开始验证
    $('#submitForm').validate({
        /* 设置验证规则 */  
        rules: {   
            CDNID: {
                required:true,
                stringCheck:true,
                stringCheck:true
            },   
    CNAME: {
                required:true,
                stringCheck:true,
                stringCheck:true
            }, 
            MOD_TIME: {   
                required:true,
                stringCheck:true,
                stringCheck:true
            }, 
        },   
           
        /* 设置错误信息 */
        messages: {   
            CDNID: {       
                required: "<font color=red>不能为空</font>",
                stringCheck: "",
                stringCheck: ""
            },
    CNAME: {       
                required: "<font color=red>不能为空</font>",
                stringCheck: "",
                stringCheck: ""
            },
    MOD_TIME: {       
                required: "<font color=red>不能为空</font>",
                stringCheck: "",
                stringCheck: ""
            },
           
        },   
               /* 设置验证触发事件 */  
        focusInvalid: false,
        onkeyup: false,
        
        /* 设置错误信息提示DOM */  
        errorPlacement: function(error, element) {       
            error.appendTo( element.parent());       
        },
           
    });
      
    });