我正在使用一些javascript代码进行验证,如下所示: -
var ErrArr = [];
$(document).ready(function () {
$('#btnSave').click(function (e) {
e.preventDefault();
validateTitle();
validateddl();
validateTextBoxes();
checkBoxChecking();
if (ErrArr.length > 0) {
alert(ErrArr.join("\n"));
ErrArr = [];
return false;
}
});
function checkBoxChecking() {
if ($("#chkDeclaration").is(":checked")) {
// alert("first button checked");
return false;
}
else {
ErrArr.push('Check the declaration');
return true;
}
}
function validateTitle() {
if ($("#ddlTitle").val() > "0") {
if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
ErrArr.push("Please enter the text in other title");
}
} else {
ErrArr.push('Please select the title');
}
}
function validateTextBoxes() {
if ($("#txt_emp_fname").val() === "") {
ErrArr.push('First name is required');
}
if ($("#txt_emp_mname").val() === "") {
ErrArr.push('Middle name is required');
}
if ($("#txt_emp_lname").val() === "") {
ErrArr.push('Last name is required');
}
if ($("#txtFatherName").val() === "") {
ErrArr.push('Father name is required');
}
if ($("#txtDateofJoin").val() === "") {
ErrArr.push('Date of joining is required');
}
if ($("#txtReligion").val() === "") {
ErrArr.push('Religion is required');
}
if ($("#txtPersonalEmail").val() === "") {
ErrArr.push('Email Id is required');
}
if ($("#txtDtOfBirth").val() === "") {
ErrArr.push('Date of birth is required');
}
if ($("#txtAt").val() === "") {
ErrArr.push('Place of birth is required');
}
if ($("#txtTaluka").val() === "") {
ErrArr.push('Taluka is required');
}
if ($("#txtPostOffice").val() === "") {
ErrArr.push('Post office is required');
}
if ($("#txtDistrict").val() === "") {
ErrArr.push('District is required');
}
if ($("#txtStatePersonal").val() === "") {
ErrArr.push('State is required');
}
if ($("#txtDtMarriage").val() === "") {
ErrArr.push('Date of Marriage is required');
}
if ($("#TxtPassportNo").val() === "") {
ErrArr.push('Passport no is required');
}
if ($("#txtIdMark").val() === "") {
ErrArr.push('Identification mark is required');
}
}
function validateddl() {
if ($("#ddlGender").val === "" || $("#ddlGender").val() == "0") {
ErrArr.push('Please select the gender');
}
if ($("#ddlMaritalStatus").val === "" || $("#ddlMaritalStatus").val() == "0") {
ErrArr.push('Please select the Marital Status');
}
if ($("#ddlNationality").val === "" || $("#ddlNationality").val() == "0") {
ErrArr.push('Please select the Nationality');
}
}
});
这里发生的是,当我使用这个javascript代码时,我的按钮点击事件不会触发。当我删除JS代码时,我得到错误
无法修改Controls集合,因为该控件包含代码块(即<%...%>)。
这是我的aspx代码: -
<asp:Button ID="btnSave" CssClass="button" Text="Save" runat="server" CausesValidation="true"
OnClick="btnSave_Click" />
请注意,我已经从堆栈和谷歌的其他链接进行了搜索和尝试,但我无法成功。
请帮助
答案 0 :(得分:1)
您应该看到这一点并使用<asp:Button OnClickClick='..' />
属性:
<asp:Button
ID="btnSave"
CssClass="button"
Text="Save"
runat="server"
CausesValidation="true"
OnClick="btnSave_Click"
OnClientClick='return performValidation();'
/>
performValidation
将成为调用先运行的主函数,您可以从中调用验证函数。如果您不想要发布页面,则应该返回false。
例如:
首先修改每个验证函数,以在验证失败时返回false:
$(document).ready(function () {
function checkBoxChecking() {
if ($("#chkDeclaration").is(":checked")) {
return false;
}
else {
ErrArr.push('Check the declaration');
return false; //return false when any validation fails.
}
}
function validateTitle() {
if ($("#ddlTitle").val() > "0") {
if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
ErrArr.push("Please enter the text in other title");
}
} else {
ErrArr.push('Please select the title');
return false;//same here. return false when fail.
}
}
//this is your main validation function..
function performValidation()
{
var ret = true;
ret = checkBoxChecking();
ret = validateTitle();
//etc
//then ..
return ret;
}
});