user190881
user190881

Reputation: 169

unable to validate using ajax control

<asp:Label ID="lblSearch" runat="server" Text="Search Prefix"></asp:Label>
                <asp:TextBox ID="txtSearch" MaxLength="2" runat="server" Width="80px"></asp:TextBox>
                <asp:RequiredFieldValidator runat="server" ID="rfvSearch" ControlToValidate="txtSearch" Display="None" ErrorMessage="Please enter &lt;b>Prefix.</b>" ValidationGroup="searchRegion"/>
                <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="vceSearch" TargetControlID="rfvSearch" HighlightCssClass="validatorCalloutHighlight" />
                <asp:RegularExpressionValidator ID="revRegion" runat="server" ErrorMessage="Please enter &lt;b>valid Numeric value.</b>"  ControlToValidate="txtSearch" Display="None" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>
                <ajaxToolkit:ValidatorCalloutExtender ID="vceRegion" runat="server" TargetControlID="revRegion" /> 

 protected void btnSearch_Click(object sender, EventArgs e)
    {

    }

I have added validation(only numeric) for txtsearch. when i enter wrong data(AA) in the textbox i am getting popup with validation message. but when i click on search button it is going to execute server side code in c#

how can i stop this and want to validate it before going to c# code in the btnSearch_Click??

when i added below code in c#

 this.Validate();
 if (IsValid)
 { }

it is not validating second time(every time isvalid is false even if it is numeric)

Can some body help me?

Upvotes: 1

Views: 44

Answers (1)

user2522503
user2522503

Reputation: 178

you can use javascript to validate your controls like below

function isNumber(evt) {
            evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                return false;
            }
            return true;
        }

        function isAlphabets(evt) {
            evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
                    return true;            
            return false;

Upvotes: 1

Related Questions