This is a migrated thread and some comments may be shown as answers.

RadInputManager OnValidation

1 Answer 50 Views
Input
This is a migrated thread and some comments may be shown as answers.
Babak
Top achievements
Rank 1
Babak asked on 23 Feb 2014, 11:19 AM
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>

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 26 Feb 2014, 03:34 PM
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.
Tags
Input
Asked by
Babak
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or