0
我一直在進行此表單驗證,當我將它顯示爲警報時,我收到了錯誤消息,但我似乎無法使用document.getElementById("").innerHTML =
打印出該表單。驗證表單錯誤消息的JavaScript
我也有這樣的,我剛纔固定它的形式
<form id="form" name="form" method="post" onSubmit="return validate(this) && reportErrors(errors)" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
var ck_name = /^(Mr|Mrs)\. (.+)? (.+)?/i;
var ck_street = /^[0-9]{2,3} +[a-z A-Z]+ +(Street|Road)$/;
var ck_postalInput = /^([D-JL-Wd-jl-w]{2})([1-9]{1})[\-\s]?([D-JL-Wd- jl-w]{1})([1-9]{2})$/;
var ck_phoneInput = /^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]* ([\d]{3})[\.\-\s]?([\d]{4})$/;
var ck_emailInput =/^[a-zA-Z]+(.)+[a-zA-Z]+(@mohawkcollege)+(.com|.ca|.org)$/;
function validate(form){
var nameInput = form.nameInput.value;
var streetInput = form.streetInput.value;
var postalInput = form.postalInput.value;
var phoneInput = form.phoneInput.value;
var emailInput = form.emailInput.value;
var errors = [];
if (!ck_name.test(nameInput)) {
errors[errors.length] = "Full name not entered correctly.";
}
if (!ck_street.test(streetInput)) {
errors[errors.length] = "Street name not entered correctly.";
}
if (!ck_postalInput.test(postalInput)) {
errors[errors.length] = "Postal not entered correctly.";
}
if (!ck_phoneInput.test(phoneInput)) {
errors[errors.length] = "Phone number not entered correctly.";
}
if (!ck_emailInput.test(emailInput)){
errors[errors.length] = "email not entered correctly.";
}
if (errors.length > 0) {
reportErrors(errors);
return false;
}
return true;
}
function reportErrors(errors){
var msg = "Please Enter Valide Data...\n";
for (var i = 0; i<errors.length; i++) {
msg += "\n" + ". " + errors[i];
}
document.getElementById("error").innerHtml=msg;
}
在'action'屬性值雙引號都沒有逃過。 –
如果解決了這個問題,請在下面添加一個答案。 – halfer