验证后发布表单
Posting a form after validation
我正在使用 validator.js 进行表单验证,并使用 MDN 包来设计我的表单样式。我在使用 node js 和 express 制作的网络应用程序中使用此表单。我不知道如何在验证后将我的表单重定向到 app.post('/example)。验证工作正常,但我无法找到将其呈现为 app.post('/example).
的方法
$(document).ready(function() {
$('.mdb-select').materialSelect();
});
(function() {
validate.extend(validate.validators.datetime, {
parse: function(value, options) {
return +moment.utc(value);
},
format: function(value, options) {
var format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss";
return moment.utc(value).format(format);
}
});
var constraints = {
email: {
presence: true,
email: true
},
password: {
presence: true,
length: {
minimum: 5
}
},
"confirm-password": {
presence: true,
equality: {
attribute: "password",
message: "^The passwords does not match"
}
},
username: {
presence: true,
length: {
minimum: 3,
maximum: 20
},
format: {
pattern: "[a-z0-9]+",
flags: "i",
message: "can only contain a-z and 0-9"
}
},
birthdate: {
presence: true,
date: {
latest: moment().subtract(18, "years"),
message: "^You must be at least 18 years old to use this service"
}
},
country: {
presence: true,
inclusion: {
within: ["SE"],
message: "^Sorry, this service is for Sweden only"
}
},
zip: {
format: {
pattern: "\d{5}"
}
},
"number-of-children": {
presence: true,
numericality: {
onlyInteger: true,
greaterThanOrEqualTo: 0
}
}
};
var form = document.querySelector("form#main");
form.addEventListener("submit", function(ev) {
ev.preventDefault();
handleFormSubmit(form);
});
var inputs = document.querySelectorAll("input, textarea, select")
for (var i = 0; i < inputs.length; ++i) {
inputs.item(i).addEventListener("change", function(ev) {
var errors = validate(form, constraints) || {};
showErrorsForInput(this, errors[this.name])
});
}
function handleFormSubmit(form, input) {
var errors = validate(form, constraints);
showErrors(form, errors || {});
if (!errors) {
showSuccess();
}
}
function showErrors(form, errors) {
_.each(form.querySelectorAll("input[name], select[name]"), function(input) {
showErrorsForInput(input, errors && errors[input.name]);
});
}
function showErrorsForInput(input, errors) {
var formGroup = closestParent(input.parentNode, "form-group"),
messages = formGroup.querySelector(".messages");
resetFormGroup(formGroup);
if (errors) {
formGroup.classList.add("has-error");
_.each(errors, function(error) {
addError(messages, error);
});
} else {
formGroup.classList.add("has-success");
}
}
function closestParent(child, className) {
if (!child || child == document) {
return null;
}
if (child.classList.contains(className)) {
return child;
} else {
return closestParent(child.parentNode, className);
}
}
function resetFormGroup(formGroup) {
formGroup.classList.remove("has-error");
formGroup.classList.remove("has-success");
_.each(formGroup.querySelectorAll(".help-block.error"), function(el) {
el.parentNode.removeChild(el);
});
}
function addError(messages, error) {
var block = document.createElement("p");
block.classList.add("help-block");
block.classList.add("error");
block.classList.add("text-danger");
block.innerText = error;
messages.appendChild(block);
}
function showSuccess() {
alert("success");
}
})();
试试这个 - 假设验证(形式,约束); returns 错误时为真。我不希望那样,所以也许您无论如何都需要撤消测试。
form.addEventListener("submit", function(ev) {
if (!handleFormSubmit(form)) ev.preventDefault();
});
和
function handleFormSubmit(form, input) {
var errors = validate(form, constraints);
showErrors(form, errors || {});
if (!errors) {
showSuccess();
}
return !errors;
}
我正在使用 validator.js 进行表单验证,并使用 MDN 包来设计我的表单样式。我在使用 node js 和 express 制作的网络应用程序中使用此表单。我不知道如何在验证后将我的表单重定向到 app.post('/example)。验证工作正常,但我无法找到将其呈现为 app.post('/example).
的方法$(document).ready(function() {
$('.mdb-select').materialSelect();
});
(function() {
validate.extend(validate.validators.datetime, {
parse: function(value, options) {
return +moment.utc(value);
},
format: function(value, options) {
var format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss";
return moment.utc(value).format(format);
}
});
var constraints = {
email: {
presence: true,
email: true
},
password: {
presence: true,
length: {
minimum: 5
}
},
"confirm-password": {
presence: true,
equality: {
attribute: "password",
message: "^The passwords does not match"
}
},
username: {
presence: true,
length: {
minimum: 3,
maximum: 20
},
format: {
pattern: "[a-z0-9]+",
flags: "i",
message: "can only contain a-z and 0-9"
}
},
birthdate: {
presence: true,
date: {
latest: moment().subtract(18, "years"),
message: "^You must be at least 18 years old to use this service"
}
},
country: {
presence: true,
inclusion: {
within: ["SE"],
message: "^Sorry, this service is for Sweden only"
}
},
zip: {
format: {
pattern: "\d{5}"
}
},
"number-of-children": {
presence: true,
numericality: {
onlyInteger: true,
greaterThanOrEqualTo: 0
}
}
};
var form = document.querySelector("form#main");
form.addEventListener("submit", function(ev) {
ev.preventDefault();
handleFormSubmit(form);
});
var inputs = document.querySelectorAll("input, textarea, select")
for (var i = 0; i < inputs.length; ++i) {
inputs.item(i).addEventListener("change", function(ev) {
var errors = validate(form, constraints) || {};
showErrorsForInput(this, errors[this.name])
});
}
function handleFormSubmit(form, input) {
var errors = validate(form, constraints);
showErrors(form, errors || {});
if (!errors) {
showSuccess();
}
}
function showErrors(form, errors) {
_.each(form.querySelectorAll("input[name], select[name]"), function(input) {
showErrorsForInput(input, errors && errors[input.name]);
});
}
function showErrorsForInput(input, errors) {
var formGroup = closestParent(input.parentNode, "form-group"),
messages = formGroup.querySelector(".messages");
resetFormGroup(formGroup);
if (errors) {
formGroup.classList.add("has-error");
_.each(errors, function(error) {
addError(messages, error);
});
} else {
formGroup.classList.add("has-success");
}
}
function closestParent(child, className) {
if (!child || child == document) {
return null;
}
if (child.classList.contains(className)) {
return child;
} else {
return closestParent(child.parentNode, className);
}
}
function resetFormGroup(formGroup) {
formGroup.classList.remove("has-error");
formGroup.classList.remove("has-success");
_.each(formGroup.querySelectorAll(".help-block.error"), function(el) {
el.parentNode.removeChild(el);
});
}
function addError(messages, error) {
var block = document.createElement("p");
block.classList.add("help-block");
block.classList.add("error");
block.classList.add("text-danger");
block.innerText = error;
messages.appendChild(block);
}
function showSuccess() {
alert("success");
}
})();
试试这个 - 假设验证(形式,约束); returns 错误时为真。我不希望那样,所以也许您无论如何都需要撤消测试。
form.addEventListener("submit", function(ev) {
if (!handleFormSubmit(form)) ev.preventDefault();
});
和
function handleFormSubmit(form, input) {
var errors = validate(form, constraints);
showErrors(form, errors || {});
if (!errors) {
showSuccess();
}
return !errors;
}