在客戶端,我想以某種方式禁用「保存」按鈕,以防未通過驗證。該解決方案驗證各個字段像這樣:如何禁用「保存」,直到字段通過驗證。 NO <form>標籤
//checking if null
$("#editAccountName").blur(function() {
var editAccountName = $("#editAccountName").val();
if (editAccountName == "" || editAccountName == null) {
$("#editAccountNameError").html('<font color="#cc0000">The account name is required</font>');
$("#editAccountName").css("background-color", "#cc0000");
}
else {
$("#editAccountNameError").html('<font color="#cc0000"></font>');
$("#editAccountName").css("background-color", "transparent");
}
});
//checking if null
$("#editAddress").blur(function() {
var editaddress = $("#editAddress").val();
if (editaddress == "" || editaddress == null) {
$("#editAddressError").html('<font color="#cc0000">The address is required</font>');
$("#editAddress").css("background-color", "#cc0000");
}
else {
$("#editAddressError").html('<font color="#cc0000"></font>');
$("#editAddress").css("background-color", "transparent");
}
});
//checking if null
$("#editCity").blur(function() {
var editCity = $("#editCity").val();
if (editCity == "" || editCity == null) {
$("#editCityError").html('<font color="#cc0000">The city is required</font>');
$("#editCity").css("background-color", "#cc0000");
}
else {
$("#editCityError").html('<font color="#cc0000"></font>');
$("#editCity").css("background-color", "transparent");
}
});
//checking if null
$("#editState").blur(function() {
var editState = $("#editState").val();
if (editState == "" || editState == null) {
$("#editStateError").html('<font color="#cc0000">The city is required</font>');
$("#editState").css("background-color", "#cc0000");
}
else {
$("#editStateError").html('<font color="#cc0000"></font>');
$("#editState").css("background-color", "transparent");
}
});
//no nulls or letters
$("#editZip").blur(function() {
var regexnumbers = /\d+-?/;
var editzip = $("#editZip").val();
if (!regexnumbers.test(editzip) == true || editzip == '' || editzip == null) {
$("#editZipError").html('<font color="#cc0000">The numeric zip code is required</font>');
$("#editZip").css("background-color", "#cc0000");
}
else {
$("#editZipError").html('<font color="#cc0000"></font>');
$("#editZip").css("background-color", "transparent");
}
});
//*optional*
//needs to be exactly 10 digits in case anything is entered in any of the boxes
$("#editArea,#editPrefix,#editSuffix").blur(function() {
var phone = $("#editArea").val() + $("#editSuffix").val() + $("#editPrefix").val();
if (phone.length > 0 && phone.length < 10) {
$("#editPhoneError").html('<font color="#cc0000">The phone number must be 10 digits</font>');
$("#editArea").css("background-color", "#cc0000");
$("#editPrefix").css("background-color", "#cc0000");
$("#editSuffix").css("background-color", "#cc0000");
}
else {
$("#editPhoneError").html('<font color="#cc0000"></font>');
$("#editArea").css("background-color", "transparent");
$("#editPrefix").css("background-color", "transparent");
$("#editSuffix").css("background-color", "transparent");
}
});
我知道這不是最乾淨的解決方案,但因爲需要我的地方<form>
標籤之間的領域(與衝突,我不能利用jQuery插件的其他功能)。標記看起來像這樣:
<label>Clinic Name</label>
<input id="editAccountName" name="editAccountName" class="accountEdit" type="text" value="@Model.Pharmacy.AccountName" /><span id="editAccountNameError" value="0"></span>
<label>Address</label>
<input id="editAddress" name="editAddress" class="accountEdit" type="text" value="@Model.Pharmacy.Address" /><span id="editAddressError" value="0"></span>
<label>City</label>
<input id="editCity" name="editCity" class="accountEdit" type="text" value="@Model.Pharmacy.City" /><span id="editCityError" value="0"></span>
<label>State</label>
<input id="editState" name="editState" class="accountEdit" type="text" value="@Model.Pharmacy.State" maxlength="2"/><span id="editStateError" value="0"></span>
<label>Zip</label>
<input id="editZip" name="editZip" maxlength="5" class="accountEdit" type="text" value="@Model.Pharmacy.ZipCode" /><span id="editZipError" value="0"></span>
<label>Phone Number (optional)</label>
<div>
<input id="editArea" maxlength="3" onkeyup="tabout(this,'editPrefix')" name="editArea" style="float:left; width:70px;" type="text" value="@Model.Pharmacy.Area" depends />
</div>
<div>
<input id="editPrefix" maxlength="3" onkeyup="tabout(this,'editSuffix')" name="editPrefix" style="float:left; width:70px;" type="text" value="@Model.Pharmacy.Prefix" depends />
</div>
<div>
<input id="editSuffix" maxlength="4" onkeyup="tabout(this,'editPrefix')" name="editSuffix" style="float:left; width:70px;" type="text" value="@Model.Pharmacy.Suffix" depends />
</div>
<span style="margin-left:-208px; margin-top:50px;" id="editPhoneError" value="0"></span>
</div>
<input id="btnCancel" type="button" value="Cancel" />
<input id="btnSave" type="button" value="Save" />
有什麼建議嗎?
或者也可以使用'.toggleClass('error')'那樣就不需要'else' –
對 - 我離開了那裏,因爲我們仍然需要它來處理錯誤消息span ...但是你絕對對,如果使用css來切換錯誤範圍的可見性,那麼不再需要if/else。 – MacPrawn
您的解決方案完美無缺!謝謝。 – julian