我有一个文本框,其中只能输入正值,如果输入负值或零,则删除该值并将文本框涂成红色,以指示用户不应输入负值。
这是我的代码:
checkForNegativeValues : function(e){
var intVal = e.currentTarget.value;
var nameOfControl = e.currentTarget.name;
// here i have to implement the logic but really don't know how to go about
e.preventDefault();
return true;
}
我可以获取值和控件名称,但不知道如何验证,我已经对表单中的所有值进行了验证,但上面的控件不属于任何控件并位于 div 中,这是因为我我正在使用backbone.marionette。
正是这样的事情:JSFIDDLE但不想打印消息,而只是用红色标记文本框。
真的很无助,不知道该怎么办。
最佳答案
以JsFiddle为例,这是你想要完成的吗?
$(document).ready(function() {
$('.abc').bind('keyup', function() {
regex = /^[A-z0-9]+$/;
if(!regex.test($(this).val())) {
// append the color red to the background
$(this).css('backgroundColor', '#f00');
} else {
// reset color to white
$(this).css('backgroundColor', '#fff');
}
});
});
关于javascript - 如何使用 jQuery 仅验证一个 HTML 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18143652/