ASP.NET Custom Validator 클라이언트 측 및 서버 측 유효성 검사가 실행되지 않음
이것은 전에 나에게 발생하지 않았지만 어떤 이유로 클라이언트 및 서버 측 유효성 검사 이벤트가 모두 트리거되지 않습니다.
<asp:TextBox ID="TextBoxDTownCity" runat="server" CssClass="contactfield" />
<asp:CustomValidator ID="CustomValidator2" runat="server" EnableClientScript="true"
ErrorMessage="Delivery Town or City required"
ClientValidationFunction="TextBoxDTownCityClient"
ControlToValidate="TextBoxDTownCity"
OnServerValidate="TextBoxDTownCity_Validate" Display="Dynamic" >
</asp:CustomValidator>
서버 측 유효성 검사 이벤트 :
protected void TextBoxDTownCity_Validate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
}
클라이언트 측 유효성 검사 이벤트 :
function TextBoxDCountyClient(sender, args) {
args.IsValid = false;
alert("test");
}
적어도 서버 측 유효성 검사가 실행될 것이라고 생각했지만 아니요. 이것은 나에게 전에 일어난 적이 없습니다. 이로 인해 정말 당황했습니다.
출력을 보았고 ASP.NET이 클라이언트 측 기능을 인식하고 있습니다.
ASP.NET JavaScript 출력 :
var ctl00_ctl00_content_content_CustomValidator2 = document.all ? document.all["ctl00_ctl00_content_content_CustomValidator2"] : document.getElementById("ctl00_ctl00_content_content_CustomValidator2");
ctl00_ctl00_content_content_CustomValidator2.controltovalidate = "ctl00_ctl00_content_content_TextBoxDTownCity";
ctl00_ctl00_content_content_CustomValidator2.errormessage = "Delivery Town or City required";
ctl00_ctl00_content_content_CustomValidator2.display = "Dynamic";
ctl00_ctl00_content_content_CustomValidator2.evaluationfunction = "CustomValidatorEvaluateIsValid";
ctl00_ctl00_content_content_CustomValidator2.clientvalidationfunction = "TextBoxDTownCityClient";
렌더링 된 맞춤 유효성 검사기 :
<span id="ctl00_ctl00_content_content_CustomValidator2" style="color:Red;display:none;">Delivery Town or City required</span>
클라이언트 및 서버 측 유효성 검사가 모두 실행되지 않는 이유에 대해 어느 누구도 밝힐 수 있습니까?
편집 : 잘못된 기능에 붙여 넣은 오타, 문제는 여전히 동일합니다.
마지막 주석에 대한 또 다른 업데이트 : by the TextBox는 비워 둘 수 없습니다. 나는 이것을 테스트했지만 사실이 아닙니다. 빈 페이지에서 CustomValidator가 값없이 내 클라이언트 측 유효성 검사 기능을 제대로 실행했습니다.
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator" ClientValidationFunction="TextBoxDAddress1Client"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
당신의 CustomValidator의지 TextBox는 비어 있지 않을 때만 발사됩니다 .
비어 있지 않은지 확인해야하는 경우 RequiredFieldValidator도 필요합니다 .
편집하다:
귀하의 경우 CustomValidator지정 ControlToValidate속성 (그리고 원래의 예를 않습니다)가 다음 유효성 검사 기능 만 컨트롤이 비어 있지 않을 때 호출됩니다.
지정하지 않으면 ControlToValidate매번 유효성 검사 함수가 호출됩니다.
이것은 문제에 대한 두 번째 가능한 해결책을 열어줍니다. 별도의을 사용하는 대신 RequiredFieldValidator에서 ControlToValidate속성을 생략하고 CustomValidator다음과 같은 작업을 수행하도록 유효성 검사 함수를 설정할 수 있습니다.
클라이언트 측 코드 (자바 스크립트) :
function TextBoxDCountyClient(sender, args) {
var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
if (v == '') {
args.IsValid = false; // field is empty
}
else {
// do your other validation tests here...
}
}
서버 측 코드 (C #) :
protected void TextBoxDTownCity_Validate(
object source, ServerValidateEventArgs args)
{
string v = TextBoxDTownCity.Text;
if (v == string.Empty)
{
args.IsValid = false; // field is empty
}
else
{
// do your other validation tests here...
}
}
이것을 사용하십시오 :
<asp:CustomValidator runat="server" id="vld" ValidateEmptyText="true"/>
빈 필드의 유효성을 검사합니다.
2 명의 검증 인을 추가 할 필요가 없습니다!
내 웹 양식에서 클라이언트 측 유효성 검사가 전혀 실행되지 않았고 이유를 알지 못했습니다. 문제는 자바 스크립트 함수의 이름이 서버 제어 ID와 동일하다는 것이 밝혀졌습니다.
그래서 당신은 이것을 할 수 없습니다 ...
<script>
function vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="vld" />
그러나 이것은 작동합니다.
<script>
function validate_vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="validate_vld" />
내부 .NET Javascript와 충돌한다고 생각합니까?
Did you verify that the control causing the post back has CausesValidation set to tru and that it does not have a validation group assigned to it?
I'm not sure what else might cause this behavior.
Also check that you are not using validation groups as that validation wouldnt fire if the validationgroup property was set and not explicitly called via
Page.Validate({Insert validation group name here});
Server-side validation won't fire if client-side validation is invalid, the postback is not send.
Don't you have some other validation that doesn't pass?
The client-side validation is not executed because you specified ClientValidationFunction="TextBoxDTownCityClient" and this will look for a function named TextBoxDTownCityClient as validation function, but the function name should be TextBoxDAddress1Client
(as you wrote)
Thanks for that info on the ControlToValidate LukeH!
What I was trying to do in my code was to only ensure that some text field A has some text in the field when text field B has a particular value. Otherwise, A can be blank or whatever else. Getting rid of the ControlToValidate="A" in my mark up fixed the issue for me.
Cheers!
'IT TIP' 카테고리의 다른 글
| git 얕은 클론 (clone --depth)이 원격 분기를 놓친다 (0) | 2020.10.30 |
|---|---|
| symfony2 doctrine query Builder를 사용하여 고유 한 쿼리를 선택하는 방법은 무엇입니까? (0) | 2020.10.30 |
| 열 (GridView)을 숨기지 만 여전히 해당 값에 액세스하는 방법은 무엇입니까? (0) | 2020.10.30 |
| 여러 dex 파일이 Lorg / apache / cordova / BuildHelper를 정의합니다. (0) | 2020.10.30 |
| 레일스 콘솔이 libreadline으로 인해로드되지 않습니다. (0) | 2020.10.30 |