2016-02-10 34 views
-3

我的代碼看起來像這樣。幫我a-z在jQuery中正則表達式不起作用

$('#form').validate({ 
    errorElement: "span", // contain the error msg in a span tag 
    errorClass: 'help-block', 
    ignore: "", 
    rules: { 
    CountryName: { 
     required: true, 
     regex: /^[a-zA-Z]+$/ 
    } 
    }, 
    messages: { 
    CountryName: { 
     required: "Enter Country name", 
     regex: "Enter Correct Country name", 
    }, 
    }, 
+1

這無關與ASP.Net MVC。據此編輯。 –

回答

2

沒有regex規則,它被稱爲pattern,距離additional-methods文件

jQuery(function($) { 
 
    $('#form').validate({ 
 
    errorElement: "span", // contain the error msg in a span tag 
 
    errorClass: 'help-block', 
 
    ignore: "", 
 
    rules: { 
 
     CountryName: { 
 
     required: true, 
 
     pattern: /^[a-zA-Z]+$/ 
 
     } 
 
    }, 
 
    messages: { 
 
     CountryName: { 
 
     required: "Enter Country name", 
 
     pattern: "Enter Correct Country name", 
 
     } 
 
    } 
 
    }); 
 
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/additional-methods.js"></script> 
 
<form id="form" method="post" action=""> 
 
    <input name="CountryName" /> 
 
    <input type="submit" value="Save" /> 
 
</form>

+0

感謝您的幫助。它的作品適合我。但它顯示默認味精「無效格式」關閉我的味精「輸入正確的國家名稱」。請告訴我需要更改嗎? –

+0

@SiddharthVarlekar,因爲在郵件中仍然使用'regex' –

+0

非常感謝你。其作品 :) –

相關問題