RadInputManager OnValidation

2 posts, 0 answers
  1. Babak
    Babak avatar
    12 posts
    Member since:
    Jan 2012

    Posted 23 Feb 2014 Link to this post

    Hi.
    I'm using RadInputManager to Validate my Controls .
    But now I have a problem , I'm gonna use radinputmanager to validate a textbox width specific javascript method . I wrote the below code, but my button do post back . I want to stop post back before validating the textbox .
    How can I do this  ?
      <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
                <script type="text/javascript">
                    function onValidating(sender, args) {
                        var nationalCode = $get(sender.get_targetControlIDs()[0]);
                        console.log(nationalCode.value);
                        if (!checkCodeMeli(nationalCode.value)) {
                            alert('the code is wrong');
                            nationalCode.focus();
                            return false;
                        }
                        else {
                            alert('The code is write ')
                            return true;
                        }
                    }
                    function pageLoad() {
                        window.$ = jQuery = $telerik.$;
                    }
     
    function checkCodeMeli(code) {
        var L = code.length;
        if (L < 8 || parseInt(code, 10) == 0) return false;
        code = ('0000' + code).substr(L + 4 - 10);
        if (parseInt(code.substr(3, 6), 10) == 0) return false;
        var c = parseInt(code.substr(9, 1), 10);
        var s = 0;
        for (var i = 0; i < 9; i++)
            s += parseInt(code.substr(i, 1), 10) * (10 - i);
        s = s % 11;
        return (s < 2 && c == s) || (s >= 2 && c == (11 - s));
        return true;
    }
                </script>
            </telerik:RadScriptBlock>
     
     
              <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="btn" runat="server" />
     
            <telerik:RadInputManager ID="RadInputManager1" runat="server">
                <telerik:TextBoxSetting InitializeOnClient="true">
                    <TargetControls>
                        <telerik:TargetInput ControlID="TextBox1" />
                    </TargetControls>
                    <ClientEvents OnValidating="onValidating" />
                </telerik:TextBoxSetting>
            </telerik:RadInputManager>
  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 26 Feb 2014 Link to this post

    Hello,

    I tested the code you have provided and validation was performed as expected when a value was entered in the TextBox control. Postback was triggered only if the input was empty when the Button was clicked.

    On order to prevent this scenario you could set the Validation-IsRequired property of the TextBoxSetting to true. This way a postback will be triggered only if there is a valid value entered in the TextBox control.

    The markup for RadInputManager would look like below:

    <telerik:RadInputManager ID="RadInputManager1" runat="server">
        <telerik:TextBoxSetting InitializeOnClient="true" Validation-IsRequired="true">
            <TargetControls>
                <telerik:TargetInput ControlID="TextBox1" />
            </TargetControls>
            <ClientEvents OnValidating="onValidating" />
        </telerik:TextBoxSetting>
    </telerik:RadInputManager>


    Regards,
    Viktor Tachev
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top