0
一個人的第二個名字需要以大寫字母開頭。如果我首先鍵入大寫字母,它會成功提交。JavaScript大寫字母驗證問題
問題是,當我輸入一個小寫字母時,將其提交,它會將錯誤消息拋出給它應該執行的用戶。雖然當我用大寫字母替換小寫字母並嘗試再次提交時,它仍然不會提交,但仍顯示用戶「必須從首都開始」。
HTML:
<form name="registrationForm" id="registrationForm" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">First Name:</td>
<td>
<input type="text" name="firstName" id="firstName">
<span id="firstNameError">*</span>
</td>
</tr>
<tr>
<td align="right">Second Name:</td>
<td>
<input type="text" name="secondName" id="secondName">
<span id="secondNameError">*</span>
</td>
</tr>
<tr>
<td align="right">Town:</td>
<td>
<input type="text" name="town" id="town">
<span id="townError">*</span>
</td>
</tr>
<tr>
<td align="right">Country:</td>
<td>
<input type="text" name="country" id="country">
<span id="countryError">*</span>
</td>
</tr>
<tr>
<td align="right">E-Mail Address:</td>
<td>
<input type="text" name="emailAddress" id="emailAddress">
<span id="emailAddressError">*</span>
</td>
</tr>
<tr>
<td align="right"></td>
<td><input type="button" id="submitRegistationForm" value="Submit"></td>
</tr>
</table>
</form>
的JavaScript:
var $ = function (id) {
return document.getElementById(id);
}
var validateRegistrationForm = function() {
var isValid = true;
//First Name Validation
if ($("firstName").value == "") {
$("firstNameError").firstChild.nodeValue = "This Field Is Required";
isValid = false;
} else {
$("firstNameError").firstChild.nodeValue = "";
}
//Second Name Validation
if ($("secondName").value == "") {
$("secondNameError").firstChild.nodeValue = "This Field Is Required";
isValid = false;
} else if ($("firstName").value[0].toUpperCase() != $("firstName").value[0]) {
$("secondNameError").firstChild.nodeValue = "Must Start With A Capital";
isValid = false;
} else {
$("secondNameError").firstChild.nodeValue = "";
}
//Town Validation
if ($("town").value == "") {
$("townError").firstChild.nodeValue = "This Field Is Required";
isValid = false;
} else {
$("townError").firstChild.nodeValue = "";
}
//Country Validation
if ($("country").value == "") {
$("countryError").firstChild.nodeValue = "This Field Is Required";
isValid = false;
} else {
$("countryError").firstChild.nodeValue = "";
}
//E-Mail Validation
if ($("emailAddress").value == "") {
$("emailAddressError").firstChild.nodeValue = "This Field Is Required";
isValid = false;
} else {
$("emailAddressError").firstChild.nodeValue = "";
}
if (isValid) {
$("registrationForm").submit();
}
}
window.onload = function() {
$("submitRegistationForm").onclick = validateRegistrationForm;
}
注意,在'其他if'條件,你檢查你的'的firstName的第一個字母'輸入,而不是你的'secondName'。 –