验证表单不起作用。您可以在下面看到代码和错误消息。不要认为是 JS 的问题,Chrome 和 Firefox 指向 HTML 部分。请参阅我从 Firefox 的 Firebug 制作的屏幕截图。
HTML:
<div>Enter your state code:<input id="state" name="state" type="text" size="2" onblur="isStateOk(this.document.getElementById("state_help"));"/>
<span id="state_help"></span></div>
Javascript:
function isStateOk(inputField, helpId) { // See if the input value contains any text
return editNodeText (/^A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]$/, inputField.value, helpId, "Enter a State Code in Uppercase (Ex.NY, PA, CA)");
}
注意:eclipse
在(“”)下显示红色虚线state_help
。也许id
根本span
找不到?虽然开始打字时我确实得到了代码帮助state_
更新: JAVASCRIPT 文件:指向以下文件中第一个函数的新错误消息:
function editNodeText(regex, input, helpId, helpMessage) { // See if the visitor entered the right information
if (!regex.test(input)) { // If the wrong information was entered, warn them
if (helpId != null)
while (helpId.firstChild) // Remove any warnings that may exist
helpId.removeChild(helpId.firstChild);
helpId.appendChild(document.createTextNode(helpMessage)); // Add new warning
return false;
} else { // If the right information was entered, clear the help message
if (helpId != null){
while (helpId.firstChild) // Remove any warnings that may exist
helpId.removeChild(helpId.firstChild);
}
return true;
}
}
//inputField – ID Number for the html text box
//helpId – ID Number for the child node I want to print a warning in
//See if the input value contains any text
function isTheFieldEmpty(inputField, helpId) {
return editNodeText(/^[A-Za-z\.\' \-]{2,15}\s?[A-Za-z\.\' \-]{2,15}\s?[A-Za-z\.\' \-]{2,15}/, inputField.value, helpId, "Please enter a valid name.");
} // inputField.value – Value typed in the html text box
function isAddressOk(inputField, helpId) { // See if the input value contains any text
return editNodeText(/^[A-Za-z0-9\.\' \-]{5,30}$/, inputField.value, helpId, "Enter a Street (Ex.1234 Main St.)");
}
function isStateOk(inputField, helpId) { // See if the input value contains any text
return editNodeText (/^A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]$/, inputField.value, helpId, "Enter a State Code in Uppercase (Ex.NY, PA, CA)");
}
function isPhoneOk(inputField, helpId) { // See if the input value contains any text
return editNodeText(/^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$/, inputField.value, helpId, "Enter a Phone Number (Ex.412-828-3000)");
}
function isEmailOk(inputField, helpId) { // See if the input value contains any text
return editNodeText(/^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/, inputField.value, helpId, "Enter an Email (Ex. cimdata@javascript.de)");
}