2013-12-16 49 views
3

我有一个使用JavaScript做表单验证和IM试图用jQuery来替换它现有的应用程序。我的表单最多有20个字段,我现在只想验证一个字段。jQuery的表单验证提交处理程序不工作

这是jQuery的功能,我写来验证一个字段。

$(function() { 

//$(".submit").click(function(){ 
// alert("jq"); 
    $("#serviceRtpLogMainForm").validate({ 
     rules: { 
      p_incdate_in: "required" 
       }, 
     messages: { 
      p_incdate_in: "Please enter the inception date." 
       }, 
     submitHandler: function(form) { 
        // do other things for a valid form 
        form.submit(); 
       } 

//}); 
}); 

而下面是如何形式,提交和有问题的领域正在验证。

<html:form action="/serviceRtpLogMain" styleId="serviceRtpLogMainForm"> 

<html:text name = "serviceRptLog" property="p_incdate_in" styleId="incidDate" onfocus="cancelRequest1('DATE')" /> 
      <A href="javascript:cal4.popup();"> 

<td valign="middle" align="left" width="100%" colspan="4" height=38 class="td3" > 
     <input type=submit value=Submit class="submit"> 
     <input type=reset value=Reset> 
    </td> 

不是我的目的是要验证只是如果字段已填写并提交只有现场已被填充以正确的格式形式。

我在这里做错了什么?

+0

我在$( “#serviceRtpLogMainForm”),在Firefox开发者工具.validate调试点。它甚至去那里..这让我更加困惑。 – DJR

回答

4

确保您的HTML会像下面,

<form id="serviceRtpLogMainForm" action="/serviceRtpLogMain"> 
    <td valign="middle" align="left" width="100%" colspan="4" height="38" class="td3" > 
     <input type="text" name="p_incdate_in" class="p_incdate_in"/> 
     <input type="submit" value="Submit" class="submit"/> 
    </td> 
</form> 

和脚本是

$(function() {   

    $("#serviceRtpLogMainForm").validate({ 
     rules: { 
       p_incdate_in: "required" 
       }, 
     messages: { 
       p_incdate_in: "Please enter the inception date." 
       }, 
     submitHandler: function(form) { 
          // do other things for a valid form 
          form.submit(); 
         } 

     }); 

     }); 

检查Fiddle

或 如果你不想使用name属性,类属性添加到您的元素,并添加规则如下图所示,

 $("#serviceRtpLogMainForm").validate({    
      submitHandler: function(form) { 
          // do other things for a valid form 
          form.submit(); 
         } 

     }); 
     $(".p_incdate_in").rules("add", { 
        required:true, 
        messages:"Please enter the inception date." 
     }); 

检查Fiddle

+0

@Subhash我的表单字段的名称属性已被使用。我可以使用styleId吗? – DJR

+0

和IM惊讶,为什么在点击提交按钮,调试点不能在Firefox开发者工具 – DJR

+0

达到@DJR检查我的编辑。 –