CiccioMiami
CiccioMiami

Reputation: 8256

Javascript validation overwrites ASP.NET validators

I have a problem that is making me crazy. On my page I have one Javascript validation and two ASP.NET validators. The validation outcome depends just on the result of the Javascript. That means if the Javascript returns true the ASP.NET validators are not checked.

The Javascript code is:

<script type="text/javascript">

    function Validate() {
        var ddlObj = document.getElementById('<%=ddStatus.ClientID%>');
        var txtObj = document.getElementById('<%=txtComment.ClientID%>');
        if (ddlObj.selectedIndex != 0) {
            if (txtObj.value == "") {
                alert("Any change of Status requires a comment!");
                txtObj.focus();
                return false;
            }
        }
    }
</script>

Instead the two ASP.NET validators are:

<td><asp:TextBox runat="server" ID="txtSerialNr" ></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="txtSerialNr" ErrorMessage="***" />
                        </td>

<td><asp:TextBox runat="server" ID="txtProdName" ></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv1" ControlToValidate="txtProdName" ErrorMessage="***"></asp:RequiredFieldValidator></td>

Anybody might help? Thanks

UPDATE:

I call the Javascript from a button:

    <asp:Button runat="server" ID="btnSubmit" Text="Save New Product" 
style="cursor:hand" OnClick="btnSubmit_Click" />

But I register the attribute from the code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    btnSubmit.Attributes.Add("OnClientClick", "return Validate()");
}

Upvotes: 0

Views: 2515

Answers (2)

James Johnson
James Johnson

Reputation: 46047

You can fire the client side validation from within the Validate() function:

validate = function(){
    bool isValid = Page_ClientValidate(""); //triggers validation
    if (isValid){
        var ddlObj = document.getElementById("<%=ddStatus.ClientID%>"); 
        var txtObj = document.getElementById("<%=txtComment.ClientID%>"); 
        if (ddlObj.selectedIndex != 0) { 
            if (txtObj.value == "") { 
                alert("Any change of Status requires a comment!"); 
                txtObj.focus(); 
                isValid = false; 
            } 
        } 
    }
    return isValid;
}

Markup:

<asp:Button runat="server" OnClientClick="return validate();" ... />

Upvotes: 1

Widor
Widor

Reputation: 13275

OK, there's a couple of things wrong here.

If you are concerned enough to do validation, you must ALWAYS do server-side validation in addition to client-side. Client-side validation is very user-friendly and fast to respond, but it can be bypassed simply by setting JavaScript to 'off'!

I don't see where you've told your controls which JavaScript function to call when they validate? You use RequiredFieldValidators which don't require an external function - but then attempt custom validation using your Validate() function.

If you do end up using a CustomValidator, then you'll need to change the 'signature' of your function. It needs to be of the form

   function validateIt(sender, args){
            var testResult = //your validation test here
            args.IsValid = testResult;
        }

Upvotes: 1

Related Questions