[英]jQuery multiple inputs with one Function RegExp
I've got a form with more than 1 inputfields. 我有一个带有1个以上输入字段的表单。 The format that should be allowed is hh:mm / h:mm.
应允许的格式为hh:mm / h:mm。 So I already have a function, that checks my input if the format is true inbstandly on input.
因此,我已经有了一个函数,该函数可以检查输入的格式是否在输入时确实为真。
So what I want is, if i click on my submit button i'd like to check all the boxes again if the format is right. 所以我想要的是,如果我点击提交按钮,那么我想再次检查所有复选框,如果格式正确。 If true then submit();
如果为true,则submit(); else then alert() or something.
否则然后alert()或其他东西。 But that isnt the problmem.
但这不是问题。
I have no idea how i can realize this. 我不知道我怎么能实现这一点。 Thank you in advance :))
先感谢您 :))
function validateAbs(inputField) {
var isValid = /^([0-1]?[0-9]|2[0-3]):([0-5][0-9])(:[0-5][0-9])?$/.test(inputField.value);
if (isValid) {
inputField.style.backgroundColor = '#bfa';
} else {
inputField.style.backgroundColor = '#fba';
}
return isValid;
}
$(function(){
$('#ist').on('input', function() {
//This is one of ne hh:mm Textboxes
validateAbs(this);
});
});
$(function(){
$('#abssubmit').on('input', function() {
//This is my Submit-Button
});
});
It's surprisingly easy: 这非常简单:
$('#abssubmit').on('input', function() {
$("selector-for-the-inputs-you-want-to-check").each(function() {
validateAbs(this);
});
});
If you want to know whether any of them is invalid, you can use filter
: 如果您想知道它们是否无效,可以使用
filter
:
$('#abssubmit').on('input', function() {
var invalidFields = $("selector-for-the-inputs-you-want-to-check").filter(function() {
return !validateAbs(this);
});
if (invalidFields.length) {
// At least one field was invalid
}
});
You will need to change to a submit handler not an 'input' handler: 您将需要更改为提交处理程序而不是“输入”处理程序:
$('#abssubmit').on('submit', function(ev) {
var isValid;
ev.preventDefault(); // to stop the form from submitting
$('#ist').each(function() {
if (!validateAbs(this)) isValid = false;
});
if (isValid) {
this.submit(); // all the validations succeeded
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.