Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an asp:textBox that accepts phone number. It is working perfectly in this way:
<asp:TextBox ID="txtPhoneNumber" onCopy="return false" onDrag="return false" onDrop="return false" onPaste="return false" onkeydown="javascript: return HandlePhoneNumber(this);"  clientidmode="Static" Font-Size="13px" Height="15px" runat="server" Width="105px" ></asp:TextBox>


Now I am trying to make this: I want to change function name of onkeydown on window.load event. How can I do this ? Below is my code:

<script type="text/javascript" language="javascript">
 window.onload = CheckCountry;
 function CheckCountry() {
          
          var country ='<%=System.Configuration.ConfigurationManager
          .AppSettings("LoginCountry").ToString() %>';
                   
          var txtPhoneNumber = $("#txtPhoneNumber");
          var maxLength = txtPhoneNumber.attr('maxLength');
          var phoneNumber = txtPhoneNumber.val();

          if (country == 'X') {
            maxLength = '14';
            phoneNumber = '963-';
            // here I need to make function name = HandlePhoneNumberX
          }
          else {
            maxLength = '12';
            phoneNumber = '961-';
           // here I need to make function name = HandlePhoneNumberY
          }
}
</script>


Any suggestion? I don't mind if you can help me to find a javascript or jQuery solution

What I have tried:

I have tried this :

if (country == 'X') {
  maxLength = '14';
  phoneNumber = '963-';
  txtPhoneNumber.on('keydown', function (e) { return HandlePhoneNumberX(e);
}
else {
  maxLength = '12';
  phoneNumber = '961-';
 txtPhoneNumber.on('keydown', function (e) { return HandlePhoneNumberY(e);
}


Nothing has worked here ...
Posted
Updated 15-Mar-17 23:51pm
v2
Comments
F-ES Sitecore 16-Mar-17 6:08am    
What's wrong with what you have?

1 solution

Its working for me. Just tried the following code. Please see if you have any syntax error at your end.
JavaScript
<script type="text/javascript" language="javascript">
    window.onload = CheckCountry;
    function CheckCountry() {

        var country = 'X';

        var txtPhoneNumber = $('#txtPhoneNumber');
        var maxLength = txtPhoneNumber.attr('maxLength');
        var phoneNumber = txtPhoneNumber.val();

        if (country == 'X') {
            maxLength = '14';
            phoneNumber = '963-';
            txtPhoneNumber.on('keydown', function (e) {
                return HandlePhoneNumberX(e, maxLength, phoneNumber);
            });
        } else {
            maxLength = '12';
            phoneNumber = '961-';
            txtPhoneNumber.on('keydown', function (e) {
                return HandlePhoneNumberY(e, maxLength, phoneNumber);
            });
        }
    }

    function HandlePhoneNumberX(e, maxLength, phoneNumber) { alert(maxLength + "-" + phoneNumber); }
    function HandlePhoneNumberY(e, maxLength, phoneNumber) { alert(maxLength + "-" + phoneNumber); }
</script>

    <asp:TextBox ID="txtPhoneNumber" onCopy="return false" onDrag="return false" onDrop="return false" onPaste="return false"  clientidmode="Static" Font-Size="13px" Height="15px" runat="server" Width="105px" ></asp:TextBox>
</asp:Content>

Also, 2 validations function might not be needed. Just merge them into 1. I have not used the onkeydown attribute in the textbox declaration.
 
Share this answer
 
v2
Comments
H.AL 16-Mar-17 5:59am    
I can't because I have to implement this function in .js file , so I can't get country there
SrikantSahu 16-Mar-17 6:13am    
Actually, you can. Have a global js field and get the country value there in the onload and use it in your js file.
H.AL 16-Mar-17 6:07am    
I think there is something incorrect by making this because even the alert is not working here
 txtPhoneNumber.on('keydown', function (e) { alert("test"); return false; }); 

and keyboard is working while false is returned
SrikantSahu 16-Mar-17 6:15am    
can you try
txtPhoneNumber[0].onkeydown = function(){alert(1);}
instead of the event handler
H.AL 16-Mar-17 6:21am    
- txtPhoneNumber[0].onkeydown = function(){alert(1);} is not working
- I will try to search for global js field and revert back ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900