我是这个javascript的新手,我正在尝试验证特定城市/县的邮政编码。当我输入正确的拉链时,我仍然会收到一条消息“仅限NM Zip,请”。我在这做错了什么?我迷路了。谢谢你的帮助。
< script language = "javascript" >
// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
var str = elem.value;
var re = /.+/;
if (str.match(re)) {
return true;
} else {
alert("Please fill in all required fields.");
return false;
}
}
//Function toggleMailAddress
function toggleMailAddress() {
if (document.selfreg.addressToggle.checked) {
document.selfreg.street2.value = document.selfreg.street.value;
document.selfreg.city2.value = document.selfreg.city.value;
document.selfreg.state2.value = document.selfreg.state.value;
document.selfreg.postalcode2.value = document.selfreg.postalcode.value;
} else {
document.selfreg.stre_haddress2.value = "";
document.selfreg.city_haddress2.value = "";
document.selfreg.stat_haddress2.value = "";
document.selfreg.post_haddress2.value = "";
}
}
function blockEnter(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode :
((evt.which) ? evt.which : evt.keyCode);
if (charCode == 13 || charCode == 3) {
return false;
} else {
return true;
}
}
// telephone number test
function isTelephoneNumber(elem) {
var str = elem.value;
var re = /^\d{3}[-. ]\d{3}[-. ]\d{4}/;
if (!str.match(re)) {
alert("Please enter your complete telephone number, with area code. You may use hyphens, periods, or spaces as separators, and you may include an extension.");
setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
return false;
} else {
return true;
}
}
// ZIP Code test
function isNMZipCode(elem) {
var str = elem.value;
var re = /^(87059,87101,87102,87103,87104,87105,87106,87107,87108,87109,87110,87111,87112,87113,87114,87115,87116,87117,87119,87120,87121,87122,87123,87124,87125,87131,87144,87151,87153,87154,87158,87165,87174,87176,87181,87184,87185,87187,87190,87191,87192,87193,87194,87195,87196,87197,87198,87199)$/;
str = str.toString();
if (str.match(re)) {
return true;
} else {
alert("NM ZIP Codes only, please.");
setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
return false;
}
}
// birth date test
function isBirthDate(elem) {
var str = elem.value;
var re = /^(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])([0-9][0-9])$/;
if (!str.match(re)) {
alert("Please check your birthdate.\nEnter the date using only six digits (mmddyy).\nUse leading zeros if necessary (for instance, 010190 for January 1, 1990).\nIf you were born before 1920, enter 010120.");
setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
return false;
} else {
return true;
}
}
// email address test
function isEmail(elem) {
if (!elem.value) { // field may be empty
return true;
} else { // if not empty, test for at sign and period
var str = elem.value;
var re = /^.+@.+\..+$/;
if (!str.match(re)) {
alert("That doesn't look like a valid email address. Please be sure you have a @ and a period.");
setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
return false;
} else {
return true;
}
}
}
// return focus to invalid entries
function focusElement(formName, elemName) {
var elem = document.forms[formName].elements[elemName];
elem.focus();
elem.select();
}
// batch validation
function validateForm(form) {
if (isNotEmpty(form.nlast)) {
if (isNotEmpty(form.nfirst)) {
if (isNotEmpty(form.stre_aaddress)) {
if (isNotEmpty(form.city_aaddress)) {
if (isNotEmpty(form.post_aaddress)) {
if (isNotEmpty(form.stre_haddress2)) {
if (isNotEmpty(form.city_haddress2)) {
if (isNotEmpty(form.stat_haddress2)) {
if (isNotEmpty(form.post_haddress2)) {
if (isNotEmpty(form.tphone1)) {
if (isTelephoneNumber(form.tphone1)) {
if (isNotEmpty(form.F051birthdate)) {
if (isBirthDate(form.F051birthdate)) {
if (isNMZipCode(form.post_haddress2)) {
if (isEmail(form.zemailaddr)) {
form.nlast.value = form.nlast.value.toUpperCase();
form.nmiddle.value = form.nmiddle.value.toUpperCase();
form.nfirst.value = form.nfirst.value.toUpperCase();
form.stre_aaddress.value = form.stre_aaddress.value.toUpperCase();
form.city_aaddress.value = form.city_aaddress.value.toUpperCase();
form.stat_aaddress.value = form.stat_aaddress.value.toUpperCase();
form.stre_haddress2.value = form.stre_haddress2.value.toUpperCase();
form.city_haddress2.value = form.city_haddress2.value.toUpperCase();
form.ddepartment.value = form.ddepartment.value.toUpperCase();
return true;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return false;
}
< /script>
<script language="JavaScript">
function toggleMailAddress() {
if (document.selfreg.addressToggle.checked) {
document.selfreg.stre_haddress2.value = document.selfreg.stre_aaddress.value;
document.selfreg.city_haddress2.value = document.selfreg.city_aaddress.value;
document.selfreg.stat_haddress2.value = document.selfreg.stat_aaddress.value;
document.selfreg.post_haddress2.value = document.selfreg.post_aaddress.value;
}
else {
document.selfreg.stre_haddress2.value = "";
document.selfreg.city_haddress2.value = "";
document.selfreg.stat_haddress2.value = "";
document.selfreg.post_haddress2.value = "";
}
}
function blockEnter(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode :
((evt.which) ? evt.which : evt.keyCode);
if (charCode == 13 || charCode == 3) {
return false;
} else {
return true;
}
}
/ / This
function is used to change lower to upper
case for the Input text
function cUpper(cObj) {
cObj.value = cObj.value.toUpperCase();
}
< /script>
<div class="pageContentInner">
<h1 class="pageHeader">Form</h1>
<div id="accessibleForm" class="form1Part">
<form method="post" name="selfreg" class="unpadded" autocomplete="off" onsubmit="return validateForm(this);">
<input type="hidden" name="TemplateName" value="bonline">
<fieldset>
<legend>Please enter the following information: (<span class="formRequired">*</span> means required field)</legend>
<table width="90%" border="0" cellspacing="0" cellpadding="5" align="center">
<tr valign="top">
<td>
<table width="90%" border="0">
<tr>
<td><span class="formLabel"><span class="formRequired">*</span>First Name:</span>
</td>
<td colspan="2">
<span class="formHelpNote"><input name="nfirst" value="" size="20" maxlength="40" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)">
use tab key or mouse to move between boxes
</span>
</td>
</tr>
<tr>
<td>Middle Name or Initial:</td>
<td colspan="2">
<span class="formHelpNote"><input name="nmiddle" value="" size="10" maxlength="40" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)"></span>
</td>
</tr>
<tr>
<td><span class="formRequired">*</span>Last Name:</td>
<td colspan="2">
<span class="formHelpNote"><input name="nlast" value="" size="40" maxlength="60" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)"></span>
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td><span class="formRequired">*</span>Mailing Street Address or PO Box:</td>
<td colspan="2">
<span class="formHelpNote"><input name="stre_aaddress" value="" size="32" maxlength="128" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)">
example: 123 Main St, or PO Box 123</span>
</td>
</tr>
<tr>
<td><span class="formRequired">*</span>Mailing City:</td>
<td colspan="2">
<span class="formHelpNote"><input name="city_aaddress" value="" size="20" maxlength="48" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)"></span>
</td>
</tr>
<tr>
<td>Mailing State:</td>
<td colspan="2">
NM
<input type="hidden" name="stat_aaddress" value="NM">
</td>
</tr>
<!--<tr>
<td><span class="formRequired">*</span>Mailing State:</td>
<td colspan="2">
<span class="formHelpNote"><input name="stat_aaddress" value="NM" size="10" maxlength="2" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)"></span>
</td>
</tr>-->
<tr>
<td><span class="formRequired">*</span>Mailing Zip Code:</td>
<td colspan="2">
<span class="formHelpNote"><input name="post_aaddress" value="" size="12" maxlength="5" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)">
must be within Albuquerque/Bernalillo County</span>
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3">
<input type="checkbox" name="addressToggle" id="addressToggle" onchange="toggleMailAddress()" onkeydown="return blockEnter(event)">My mailing address is the same as my residence address.
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3">Residence address:</td>
</tr>
<tr>
<td>Residence Street Address (no PO Boxes, please):</td>
<td colspan="2">
<span class="formHelpNote"><input name="stre_haddress2" value="" size="32" maxlength="128" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)">
example: 123 Main St</span>
</td>
</tr>
<tr>
<td>Residence City:</td>
<td colspan="2">
<span class="formHelpNote"><input name="city_haddress2" value="" size="20" maxlength="48" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)"></span>
</td>
</tr>
<td>Residence State:</td>
<td colspan="2">
<span class="formHelpNote"><input name="stat_haddress2" value="NM" size="10" maxlength="2" onkeydown="return blockEnter(event)" onkeyup="return cUpper(this)"></span>
<!--<tr>
<td>Residence State:</td>
<td colspan="2">
CA
<input type="hidden" name="stat_haddress2" value="NM">
</td>
</tr>-->
<tr>
<td>
</span>Residence Zip Code:</td>
<td colspan="2">
<span class="formHelpNote"><input name="post_haddress2" value="" size="12" maxlength="5" onkeydown="return blockEnter(event)"></span>
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td><span class="formRequired">*</span>Phone Number:</td>
<td colspan="2">
<span class="formHelpNote"><input name="tphone1" value="" size="16" maxlength="30" onchange="isTelephoneNumber(this)" onkeydown="return blockEnter(event)">
example: 415-555-5555</span>
</td>
</tr>
<tr>
<td>Alternate Phone Number:</td>
<td colspan="2">
<span class="formHelpNote"><input name="pphone2" value="" size="16" maxlength="30" onchange="isTelephoneNumber(this)" onkeydown="return blockEnter(event)">
example: 415-555-5555</span>
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td><span class="formRequired">*</span>Birthdate:</td>
<td colspan="2">
<span class="formHelpNote"><input name="F051birthdate" value="" size="8" maxlength="6" onchange="isBirthDate(this)" onkeydown="return blockEnter(event)">
mmddyy; example: 053185 for May 31, 1985</span>
</td>
</tr>
</table>
<br />After clicking the Submit button, please wait for the confirmation screen.
<br />
<br />
<input type="submit" value="Submit" border="0" class="button">
<br />
<br />
</form>
<!--startover-->
<!--End of main page content-->
</td>
</tr>
</table>
</fieldset>
</div>
<!--accessibleForm -->
</div>
<!-- pageContentInner -->
<!--End the main content table-->
<br />
</div>
<!-- pageContent -->
<div style="clear:both"></div>