locked
Validation failure RRS feed

  • Question

  • User1536465747 posted

    I have the following code:

    <asp:Label runat="server" AssociatedControlID="txtPhoneNumber" Text="<%$Resources:Res,Label_Phone %>" />
    <telerik:RadMaskedTextBox runat="server" ID="txtPhoneNumber" RenderMode="Lightweight" Skin="MetroTouch" Width="100%" DisplayMask="(+1) ### ### ## ##" Mask="(+1) ### ### ## ##" PromptChar="_" SelectionOnFocus="CaretToBeginning" RequireCompleteText="false" CausesValidation="true" ValidationGroup="Account" />
    <br />
    <asp:CustomValidator runat="server" ID="reqPhone" ErrorMessage="<%$Resources:Res,Alert_RequiredField %>" ControlToValidate="txtPhoneNumber" ClientValidationFunction="RequiredPhone" OnServerValidate="reqPhone_ServerValidate" ValidationGroup="Account" ForeColor="Red" Display="Dynamic" SetFocusOnError="true" />
    <telerik:RadScriptBlock runat="server">
    <script>
        function RequiredPhone(sender, args) {
            if (args.Value.length == 17 || args.Value.length == 18) {
                args.IsValid = true;
             }else {
                args.IsValid = false;
             }
        }
    </script>
    </telerik:RadScriptBlock>

    As you see I need to validate the phone number but unfortunately, this custom validator does not work at the beginning. Only if you type one or more digits and click the submit button it is invoked. Otherwise, it behaves like that the phone # is valid. 

    Any ideas? Thank you so much

    Monday, August 27, 2018 7:25 AM

All replies

  • User475983607 posted

    Use a required field validator.  Otherwise, seek Telerik support for information on how their controls work.

    Monday, August 27, 2018 11:11 AM
  • User-1171043462 posted

    If you want to check for Blank also then change as follows

    <script>
        function RequiredPhone(sender, args) {
            if (args.Value.length > 0 && (args.Value.length == 17 || args.Value.length == 18)) {
                args.IsValid = true;
             }else {
                args.IsValid = false;
             }
        }
    </script>

    Monday, August 27, 2018 3:35 PM