0
votes

I have an ASP.NET TextBox with a CustomValidation control that invokes client side validation script.

<asp:TextBox ID="txtSubsContrRbtAmt" runat="server" 
                        CssClass="textEntry NumericInput" Width="150px"
                        Text="" onKeyUp="SumValues();" MaxLength="16"></asp:TextBox>


<asp:CustomValidator ID="cvalSubsContrRbtAmt" runat="server" ClientValidationFunction="ValidatetxtSubsContrRbtAmt"
                        ControlToValidate="txtSubsContrRbtAmt" CssClass="errlable" ErrorMessage="Max Decimals = 7"
                        SetFocusOnError="True" ValidationGroup="CarbsAdd"></asp:CustomValidator>

Here's the Client script:

function ValidatetxtSubsContrRbtAmt(source, args) {

    var txtSubsContrRbtAmt = document.getElementById("<%=txtSubsContrRbtAmt.ClientID%>");
    var amount = txtSubsContrRbtAmt.value;

    args.IsValid = ValidAmount(amount);

    if (!args.IsValid)
        txtSubsContrRbtAmt.focus();
}

function ValidAmount(amount) {

    if (isNumber(amount)) {
        return (RoundToXDecimalPlaces(amount, 7) == amount);
    }
    else {
        return true;
    }  

In the ValidatetxtSubsContrRbtAmt function, the "source" parameter is the CustomValidator. That control has a property "ControlToValidate." If I can get to it, I can programmatically retrieve the value from that control and not have to have a separate function to validate each textbox.

jQuery is too much for me at this point, I'm looking for a plain old Javascript approach, please.

2

2 Answers

0
votes

You don't have to get the text box. You can get the value from args.Value. The focus should be set automatically if you set SetFocusOnError="true".

function ValidatetxtSubsContrRbtAmt(source, args) {

    var amount = args.Value;

    args.IsValid = ValidAmount(amount);
}
0
votes

You should be able to get to the control from the source object.

function ValidatetxtSubsContrRbtAmt(source, args) {
    var controlToFocusOn = source.ControlToValidate;

you can switch that out with "document.getElementByID()" to get the ID or whatever attribute you need

    var controlId = document.getElementById(source.ControlToValidate).id;
}

now you can focus or do what you need with the control. I had to access the the actual ControlToValidate earlier today from a CustomValidator.