【发布时间】:2011-11-01 12:12:15
【问题描述】:
嘿!
我在此链接上检查了输入掩码http://digitalbush.com/projects/masked-input-plugin/ 但是当用户离开文本框时我想要一些模糊的东西然后它验证格式是否为 999,99 如果用户没有输入 999,99 然后它显示消息来纠正它。否则不行。谁能给个提示
【问题讨论】:
标签: jquery jquery-ui jquery-plugins jquery-selectors
嘿!
我在此链接上检查了输入掩码http://digitalbush.com/projects/masked-input-plugin/ 但是当用户离开文本框时我想要一些模糊的东西然后它验证格式是否为 999,99 如果用户没有输入 999,99 然后它显示消息来纠正它。否则不行。谁能给个提示
【问题讨论】:
标签: jquery jquery-ui jquery-plugins jquery-selectors
@Stuvie 得到了一些东西 :)
$('input').focusout(function(e){
var error = false;
var regex = /^\d+,\d{2,2}$/;
if(!($('#checkval').val().match(regex))) {
error = true;
}
if(error == true) {
$('#result').html("<em>ERROR</em> Input must be deciaml format '99,00'");
} else {
$('#result').html("");
}
});
【讨论】:
var regex2 = /^\d{2,2},\d{2,2}$/;
单独使用 jQuery,您可以使用正则表达式实现您想要的。使用起来可能不太好,但正则表达式可能非常强大......
// either create a hidden error message in the HTML
// or create an error message element here...
var numberMaskError = $(".number-mask-error");
var numberMaskInput = $(".number-mask");
function validateNumberMask() {
// using a regular expression to determine validity
// matches a pattern with one or more digits 0-9 followed by a comma
// followed by exactly two more digits 0-9
if(numberMaskInput.val().search(/^\d+,\d{2,2}$/) !== -1) {
// hide or remove the error
numberMaskError.hide();
} else {
// show or append the error
numberMaskError.show();
}
}
numberMaskInput.bind("change", function() {
// validate the field when the value has been changed on blur
validateNumberMask();
}).bind("keyup", function() {
// revalidate if the error is currently showing and user is changing the value
if(err.is(":visible")) {
validateNumberMask();
}
});
希望对你有帮助
【讨论】: