我有一个CustomValidator,用于验证几种不同电话号码方案的电话号码.客户端javascript看起来像这样:
validatePhoneNumber(sender,args) { cleanNumber = args.Value.replace(/\D/,""); country = $("#" + CountryID).get(0).value; switch (country) { case "North America": args.IsValid = validateNAPhoneNumber(cleanNumber); if (!args.IsValid) sender.errormessage = "* Not a NA Phone #"; break; case "UK": args.IsValid = validateUKPhoneNumber(cleanumber); if (!args.IsValid) sender.errormessage = "* Not a UK Phone #"; break; ... } }
实际验证正确进行,CustomValidator始终具有正确的IsValid属性.然而,sender.errormessage似乎在此函数调用它的默认值之后被重写.如何更改errormessage值,并使其“坚持”?
解决方法
function dateofbirth(sender,args) { var dtcDOB = document.getElementById('<%= dtcDOB.ClientID %>'); var dob = new Date(dtcDOB.value); var currDate = new Date(); if (dtcDOB.value == "") { args.IsValid = false; sender.textContent = "Provide DOB."; return; } args.IsValid = true; }
尝试sender.textContent =“你的错误信息”.它对我有用.