Natural cursor position in RadMaskedTextBox

4 posts, 0 answers
  1. Clarence Klopfstein
    Clarence Klopfstein avatar
    13 posts
    Member since:
    Oct 2008

    Posted 24 Sep 2009 Link to this post

    I have a masked text box for a phone number (###) ###-####.

    But when the user clicks on the box it select whereever they clicked.  Is there a way to make it act like a text box and go ot the far left place?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 25 Sep 2009 Link to this post

    Hi Clerence,

    Try setting the property SelectionOnFocus="CaretToBeginning" and see whether it works as expected.

    aspx:
     
    <telerik:RadMaskedTextBox ID="RadMaskedTextBox1" runat="server" DisplayMask="(###) ###-####" 
        Mask="(###) ###-####" SelectionOnFocus="CaretToBeginning"
    </telerik:RadMaskedTextBox> 

    -Shinu.
  3. Clarence Klopfstein
    Clarence Klopfstein avatar
    13 posts
    Member since:
    Oct 2008

    Posted 25 Sep 2009 Link to this post

    This helped a little.  If I go to the box via tab, it goes to the far left spot.  However if I click on the box it goes to the end, or to the point I click.  Would love if it auto determined where to go... like if I had (51_) ___-____ it would go to that third spot when I clicked on the box.
  4. Dimo
    Admin
    Dimo avatar
    8450 posts

    Posted 28 Sep 2009 Link to this post

    Hello Clarence,

    I am afraid that such an auto-detecting behavior for the caret position is currently not supported. We may consider implementing it in the future. Sorry if this causes you inconvenience.

    You can try using something like the following. MyFocus2 works better, but uses hardcoded numbers, which depend on the mask.

    <%@ Page Language="C#" %> 
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     
    <html xmlns="http://www.w3.org/1999/xhtml" > 
    <head runat="server"
    <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
    <title>RadControls for ASP.NET AJAX</title> 
    </head> 
    <body> 
    <form id="form1" runat="server"
    <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
     
    <telerik:RadMaskedTextBox ID="RadMaskedTextBox1" runat="server" Mask="(###)-##-######" Text="51" SelectionOnFocus="CaretToBeginning"
        <ClientEvents OnFocus="MyFocus2" /> 
    </telerik:RadMaskedTextBox> 
     
    <script type="text/javascript"
     
    function MyFocus1(sender, args) 
        if (sender.get_value() != "") 
        { 
            if (sender.get_valueWithLiterals().indexOf("--") != -1) 
            { 
                sender.set_caretPosition(sender.get_value().length + 1); 
            } 
            else if (sender.get_valueWithLiterals().indexOf("-") != -1) 
            { 
                sender.set_caretPosition(sender.get_valueWithLiterals().length); 
            } 
            else 
            { 
                sender.set_caretPosition(sender.get_value().length + 1); 
            } 
        } 
     
    // or 
     
    function MyFocus2(sender, args) 
        var v = sender.get_value(); 
        document.title = v.length + " " + v; 
        if (v != "") 
        { 
            if (v.length < 3
            { 
                sender.set_caretPosition(v.length + 1); 
            } 
            else if (v.length < 5
            { 
                sender.set_caretPosition(v.length + 3); 
            } 
            else 
            { 
                sender.set_caretPosition(v.length + 4); 
            } 
        } 
     
    </script> 
     
    </form> 
    </body> 
    </html> 


    All the best,
    Dimo
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top