2013-02-20 62 views
0

我有安裝程序,其中三個獨立的目標需要實現:jQuery驗證 - 如何忽略內嵌模式驗證

  1. 客戶端驗證(這是使用jQuery驗證插件完成)
  2. 純數字(使用jQuery autoNumeric插件完成)
  3. 在僅限數字的字段上彈出的iPad/iPhone鍵盤(這是通過在輸入元素上使用pattern =「[0-9] *」屬性完成的 - 我無法使用HTML5 type =「number」,因爲autoNumeric插件不支持它)。

的問題是,雖然它的工作原理,則驗證插件引起誤差的浮點值(無效格式),作爲小數分隔不匹配圖案=「[0-9] *」的正則表達式。請在下面找到一個樣本輸入字段:

<input type="text" id="acme" name="acme" size="4" 
data-autonumeric="{vMin: '0.00'}" class="required number autoNumeric" 
min="0.0" step="0.01" pattern="[0-9*]"> 

有沒有辦法以某種方式禁用模式驗證,最好的全球範圍內,對所有驗證字段,或者每場?或者也許有另一種方法來強制在iPad/iPhone上的數字小鍵盤,這並不意味着type =「number」或pattern =「[0-9 *]」?

編輯:

我找到解決這個問題的方式,請參閱我的回答如下。如果有人有更好的主意,請分享。謝謝。

+0

請出示你的代碼... – Sparky 2013-02-20 02:52:26

回答

0

我發現了一種方式,通過覆蓋jQuery Validate中的模式驗證方法。如果有人知道更好的解決方案,請評論。

這是代碼。它適用於jQuery.validator.addMethod(「模式」...添加(忽略\ d * - 在iOS中顯示數字鍵盤,可以將pattern屬性的值設置爲「[0-9] 」或「\ d」,我用(來源http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html)。註釋掉線在行動中看到的問題。

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script> 
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/additional-methods.min.js"></script> 
<script type="text/javascript" src="jquery.metadata.js"></script> 
<script type="text/javascript" src="autoNumeric-1.7.4.js"></script> 
<form id="test"> 
<input type="text" id="acme" name="acme" size="4" data-autonumeric="{vMin: '0.00'}" 
    class="required number autoNumeric" min="0.0" step="0.01" pattern="\d*" value="1.00"> 
</form> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#test").validate(); 
    }); 

    // This overrides the additional-methods.min.js implementation to ignore pattern="\d*" validation 
    jQuery.validator.addMethod("pattern", function (e, t, n) { 
     return this.optional(t) || n == "\\d*" ? !0 : (typeof n == "string" && (n = new RegExp("^(?:" + n + ")$")), n.test(e)) 
    }, "Invalid format."); 
</script>