user1937256
user1937256

Reputation: 23

Client Side Custom Validation isn't executing

I'm currently in the process of learning ASP.NET 4.0 with c#. I have spent roughly 2 hours researching this issue and I don't currently have a high enough understanding of how it all works to apply it to my own situation so I require 1 on 1 assistance please. Also It's also very likely that my error is very amateur so please don't be afraid to explore that possibility. Finally, thank you for your input

I have made a very basic form that just takes text input for a number of fields (name, email, confirm email, home phone, business phone.) with validation on all fields. All of my validation works except I can't seem to figure out why my client side javascript custom validation (which is simply a requirement that either the home phone field or the business phone field has input) doesn't do anything. Server-side validation works fine (I get the desired results if I post to the server with every other field correct but the custom validation doesn't do anything if one or more other fields are invalid.)

I will try and post all relevant code needed to troubleshoot: My javascript that sets if the validation is valid if either textbox's have text

<script type="text/javascript">
    function ValidatePhoneNumbers(source, args)
    {
        var phoneHome = document.getElementById('<%= PhoneHome.ClientID %>');
        var phoneBusiness = document.getElementById('<%= PhoneBusiness.ClientID %>');
        if (phoneHome.value != '' || phoneBusiness != '')
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }   
    }
</script>

This is my custom validation mark up:

<asp:CustomValidator ID="CustomValidator1" runat="server" 
                ClientValidationFunction="ValidatePhoneNumbers" CssClass="ErrorMessage" 
                Display="Dynamic" ErrorMessage="Enter Your home or business phone number" 
                onservervalidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>

Finally these are my two textboxes:

<asp:TextBox ID="PhoneHome" runat="server"></asp:TextBox>

<asp:TextBox ID="PhoneBusiness" runat="server"></asp:TextBox>

Upvotes: 0

Views: 49

Answers (1)

Martin Parenteau
Martin Parenteau

Reputation: 73761

There is a small mistake in your Javascript function, which causes the validation to always succeed:

if (phoneHome.value != '' || phoneBusiness != '')

should be replaced by:

if (phoneHome.value != '' || phoneBusiness.value != '')

Upvotes: 1

Related Questions