locked
Is it really possible to use the MaskedEditExtender for this kind of phone numer RRS feed

  • Question

  • User1366894300 posted

    I have this textBox control  <asp:TextBox ID="txtInformerPhone" runat="server" />
    This input is a telephone number and has the following restrictions.

    A valid phone number must have the following requirements. First part of the phone number is either 2 figures or 3 figures or 4 figures. The second part is
    either 5 figures or 6 figures or 7 figures or 8 figures or 9 figures.
    So the user can enter for example 123-12345
    This is valid because the first part containd 3 figures which is valid the second part contains  5 figures which is also valid.
    Note the user must also enter this character - that is located between the first part and the second part.

    I just wonder is it possible to use MaskedEditExtender for this testBox when I have this requirements

    //Tony

    Friday, January 17, 2014 3:25 PM

Answers

  • User-417640953 posted

    Hi Tojo,

    Thank you post the issue to asp.net forum.

    According to your description, I think you want to mask the phone number input text using MaskedEditExtender.

    For this issue, I think you can mask the input text just using MaskedEditExtender without validation.

    Then validate the input value using javascript when it losed focus.

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
            $(function () {
                $("#mainPhone").blur(function () {
    
                    var phonenum = $("#mainPhone").val().replace("_", "").replace("_", "");
                    //your code to validate the phonenum....
                });
               
            })
        </script> 
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
     <asp:TextBox runat="server" ID="mainPhone"></asp:TextBox><br />
     <asp:MaskedEditExtender ID="maskMainPhone" runat="server" TargetControlID="mainPhone"
            Mask="999-99999999" MaskType="None" InputDirection="LeftToRight" ClearMaskOnLostFocus="false" AutoComplete="false" />
     <asp:Button ID="Button1" runat="server" Text="Button" />

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2014 12:11 AM

All replies

  • User2103319870 posted

    Hi,

    You best option is to use a Regular expressions for this type of validations. If you are ok with using Regular expression then you can try the below approach.

    Regular Expression:

    ^\d{2,4}[-]\d{5,9}$

    Explanation:

    ^        #  Beginning of Regular Expression
    \d{2,4}  #  Any numbers with in a range of 2 - 4 in count
    [-]      #  Hyphen in between
    \d{5,9}  #  Any numbers with in a range of 5 - 9 in count
    $        #  End on Regular Expression

    You can easily use the regular expression in Asp.Net Regualar Expression Validator

     <asp:TextBox ID="txtPhoneNumber" runat="server" TabIndex="2"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtPhoneNumber"  ValidationExpression="^\d{1,4}[-]\d{5,9}$" runat="server" ErrorMessage="Please enter a valid number in correct format"></asp:RegularExpressionValidator>

    Hope this helps

    Edit: Corrected the Regular Expression

    Friday, January 17, 2014 9:38 PM
  • User1366894300 posted

    I use regular expression now but had hopped that I could show the textBox like a pattern like this
    ____-_____
    so that the user can see how the pattern look like and doesn't have to enter the - character.
    In addition I could use the FilteredTextBoxExtender to prevent all character except number.
    I now use FilteredTextBoxExtender but must allow - because this must also be entered.

    //Tony

    Saturday, January 18, 2014 5:09 AM
  • User-417640953 posted

    Hi Tojo,

    Thank you post the issue to asp.net forum.

    According to your description, I think you want to mask the phone number input text using MaskedEditExtender.

    For this issue, I think you can mask the input text just using MaskedEditExtender without validation.

    Then validate the input value using javascript when it losed focus.

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
            $(function () {
                $("#mainPhone").blur(function () {
    
                    var phonenum = $("#mainPhone").val().replace("_", "").replace("_", "");
                    //your code to validate the phonenum....
                });
               
            })
        </script> 
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
     <asp:TextBox runat="server" ID="mainPhone"></asp:TextBox><br />
     <asp:MaskedEditExtender ID="maskMainPhone" runat="server" TargetControlID="mainPhone"
            Mask="999-99999999" MaskType="None" InputDirection="LeftToRight" ClearMaskOnLostFocus="false" AutoComplete="false" />
     <asp:Button ID="Button1" runat="server" Text="Button" />

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2014 12:11 AM