jquery - Phone Mask in ASP.Net -


i doing asp.net application , need use phone mask input in textbox. using jquery , not working.

asp.net:

<asp:textbox runat="server" id="txtcelular" cssclass="w240 placeholder"></asp:textbox> 

javascript:

    <script src="\scripts\jquery-1.7.2.js" type="text/javascript"></script> <script type="text/javascript">     $("#txtcelular").mask("(999) 9999-9999?9");       $("#txtcelular").on("blur", function () {         var last = $(this).val().substr($(this).val().indexof("-") + 1);          if (last.length == 5) {             var move = $(this).val().substr($(this).val().indexof("-") + 1, 1);              var lastfour = last.substr(1, 4);              var first = $(this).val().substr(0, 9);              $(this).val(first + move + '-' + lastfour);         }     }); </script> 

you have keep in mind id specify on elements on server, default, not id used on client. , since javascript runs on client, it's not getting right element because of id mismatch. have number of options. trips new web forms developers when start using master pages, templated controls, or user controls (.ascx).


change client id mode approach

you can change clientidmode element (this can done @ page or web.config levels).

<asp:textbox runat="server" id="txtcelular" clientidmode="static" cssclass="w240 placeholder" /> 

embed client id approach

you can embed client id in javascript, long javascript directly in web forms page or control.

$("#<%= txtcelular.clientid %>").mask("(999) 9999-9999?9"); 

use class approach

or in situations (this candidate), class makes more sense.

<asp:textbox runat="server" id="txtcelular" cssclass="w240 placeholder phonemask" />  $(".phonemask").mask("(999) 9999-9999?9"); 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -