[英]jQuery Validation with regex
我使用jQuery Validation Plugin來驗證電子郵件,用戶名,密碼...,並且我嘗試添加一些正則表達式,但這是行不通的。 有人能幫我嗎? 非常感謝。 這是我嘗試添加的正則表達式的圖片
您可以使用“模式:”來匹配正則表達式。 下面的代碼顯示了字符串的正則表達式匹配。 希望它能達到您的目的。
$("#your_form").validate({
rules: {
yourelementName: {
required:true,
pattern: /^[a-zA-Z'.\s]{1,40}$/ //this line validates the element with the provided regex expression
},
},
messages: {
yourelementName: {
required:'Please enter the data'
pattern: 'The Textbox string format is invalid'
}
}
});
$('#name').focusout(function () {
var pattern = /^[a-zA-Z ]{2,30}$/;
if (pattern.test($("#name").val())) {
$("#name").css('border-color',
'green') v1 = 1
} else {
$("#name").css('border-color', 'red') v1 = 0
}
});
這樣做,請使用您要驗證的事件來驗證文本框。
如果可以解決問題,請嘗試以下代碼
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Your input."
);
確實如下驗證
$("#field").rules("add", { regex: "^[a-zA-Z'.\\s]{1,40}$" })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.