2011-05-20 278 views
2

我有一種形式,其中一個輸入類型的值是「名字」。但是,這可以在聚焦狀態的功能被改變我想爲這個輸入字段驗證它是否爲空或「名」JQuery驗證不起作用

我有兩個jQuery的文件jquery-1.4.2.min.js & jquery.validate.pack.js

我對這種形式的另一個jQuery的文件:

jQuery(document).ready(function() { 
    jQuery("#frmRegister").validate({ 
     errorElement:'div', 
     rules: { 
      Fname:{ 
       required:true, 
       minlength: 2, 
       maxlength:30    
      } 
     }, 
     messages: { 
      Fname:{ 
       required: "Please enter first name", 
       minlength: "Required minimum 2 characters allowed", 
       maxlength: "Required maximum 30 characters allowed" 
      } 
    }); 
    jQuery("#msg").fadeOut(5000); 
}); 

在這個文件中required:true的工作,如果值爲空,但默認值是「名」,所以它不工作,我想這兩個如果是空白或是「名字」。

<form name="frmRegister" id="frmRegister" method="post"> 
    <ul class="reset ovfl-hidden join"> 
     <li class="fall"> 
      <label for="Fname">Full Name:</label> 
      <div class="fl"> 
       <input type="text" class="form" id="Fname" name="Fname" value="First Name" onfocus="if(this.value=='First Name')this.value='';" onblur="if(this.value=='')this.value='First Name';" /> 
      </div> 
     </li> 
    </ul> 
</form> 

請儘早回覆。

謝謝。

+3

「請儘早回覆」?真? – jwueller 2011-05-20 10:20:47

+2

@快速,快速,快速,快速,時間的金錢! :P – martynthewolf 2011-05-20 10:22:58

回答

0

這是我的代碼,它的正常工作:試試這個

$().ready(function() { 
$("#form2").validate(); 
// validate signup form on keyup and submit 
$("#form1").validate({ 
    rules: { 
userid: "required", 
username: { 
    required: true, 
    minlength: 2 
}, 
password: { 
    required: true, 
    minlength: 5 
}, 
authority: { 
     required: true 
    }, 
emailid: { 
    required: true, 
    email: true 
    } 
}, 
    messages: { 
    userid: "Please enter your user Id", 
    username: { 
     required: "Please enter a username", 
     minlength: "Your username must consist of at least 2 characters" 
     }, 
    password: { 
     required: "Please provide a password", 
     minlength: "Your password must be at least 5 characters long" 
     }, 
     emailid: "Please enter a valid email address", 
     authority:"Please select Authority" 
     }, 
    } 

}); 
0

難道ü嘗試的添加自定義驗證]

jQuery.validator.addMethod("workPublicserviceDate", 
    function(value, element) { 

    var hint = '<?php echo $dateHint; ?>'; 
    var format = '<?php echo $format; ?>'; 
    var pubService = strToDate($('#txtAPS').val(), format) 
    var dateAssume = strToDate($('#txtAssumeDate').val(), format); 

    if (pubService && dateAssume && (pubService > dateAssume)) { 
     return false; 
    } 
     return true; 
    }, "" 
); 

請注意:我已經添加例如

$("#frmEmpJobDetails").validate({ 
        rules: { 
txtAPS: { workPublicserviceDate: function(){ return ['<?php echo $dateHint; ?>','<?php echo $format; ?>']}, required:true }, 
} 
messages: { 
txtAPS : { workPublicserviceDate: '<?php echo __("Invalid date."); ?>', 
} 
0

你可以有需要的這樣的自定義代碼:

jQuery(document).ready(function() { 
jQuery("#frmRegister").validate({ 
    errorElement:'div', 
    rules: { 

     Fname:{ 
      required: { 
       depends: function(element) { 
        return (($("#Fname").val() == "First Name") || ($("#Fname").val() == "")); 
       } 
      }, 
      minlength: 2, 
      maxlength:30    

     } 

    }, 
    messages: { 
     Fname:{ 
      required: "Please enter first name", 
      minlength: "Required minimum 2 characters allowed", 
      maxlength: "Required maximum 30 characters allowed" 
     } 
}); 
jQuery("#msg").fadeOut(5000); 
}); 

我這裏假設你正在驗證元素的ID其中 「fname」。