locked
MaskedEditExtender behavior RRS feed

  • Question

  • User560039620 posted

    I am referencing the latest AJAXControlToolkit in my ASP.Net 4.0.xxx WebForm application. I am using the extender to format a phone number field as follows. If the field is left empty then I don't need any mask showing up. But if the phone number is entered then I don't want the mask to disappear when tabbed away from that field, but this is exactly whats happening. The hyphens in the mask disappear and only numbers are left in the field. How can I prevent this from happening?

    <asp:TextBox ID="Contact_HomePhone" runat="server" MaxLength="50"></asp:TextBox>
    
    <ajaxToolkit:MaskedEditExtender
    Mask="999-999-9999" MaskType="Number" ClearMaskOnLostFocus="True"
    ID="Contact_HomePhone_MaskedEditExtender" runat="server" TargetControlID="Contact_HomePhone" />

    Thanks.

    Friday, January 15, 2016 4:37 PM

Answers

  • User560039620 posted

    I am going with jQuery MaskedInput code and it is working exactly as I wanted and even more.

    Thanks for all the feedback!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 27, 2016 1:29 AM

All replies

  • User2103319870 posted

    But if the phone number is entered then I don't want the mask to disappear when tabbed away from that field, but this is exactly whats happening. The hyphens in the mask disappear and only numbers are left in the field. How can I prevent this from happening?
    Mask="999-999-9999" MaskType="Number" ClearMaskOnLostFocus="True"

    If you want hypens to stay even after focus is lost form text then you need to set ClearMaskOnLostFocus to false

     <ajaxToolkit:MaskedEditExtender 
       Mask="999-999-9999" MaskType="Number" ClearMaskOnLostFocus="false"
       ID="Contact_HomePhone_MaskedEditExtender" runat="server" TargetControlID="Contact_HomePhone"  />

    Friday, January 15, 2016 5:03 PM
  • User560039620 posted

    Thanks a2h.

    I can set ClearMaskOnLostFocus to false, but when the user is tabbing through the fields and then come upon this phone number field and decided not to enter anything and then tabbed off (lost focus), then the empty mask is left behind. We don't prefer hyphens and dashes only in phone fields saved to the database. I know I can intercept and clean such fields before saving, but this will be an extra work. I was hoping if something was built into the toolkit controls that would help avoid this problem.

    Thanks.

    Friday, January 15, 2016 5:22 PM
  • User61956409 posted

    Hi aamirghanchi,

    when the user is tabbing through the fields and then come upon this phone number field and decided not to enter anything and then tabbed off (lost focus), then the empty mask is left behind. We don't prefer hyphens and dashes only in phone fields saved to the database.

    You could try to use the following code to get the text of Contact_HomePhone before you insert records into database.

    string str = Contact_HomePhone.Text.ToString() == "___-___-____" ? "" : Contact_HomePhone.Text.ToString();

    Best Regards,

    Fei Han

    Wednesday, January 20, 2016 9:03 AM
  • User560039620 posted

    I am going with jQuery MaskedInput code and it is working exactly as I wanted and even more.

    Thanks for all the feedback!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 27, 2016 1:29 AM