How to use external Js file in page and pass control value to Js file using JavaScript in ASP.Net

Last Reply on Mar 14, 2017 04:04 AM By AnandM

Posted on Mar 09, 2017 02:05 AM

 

<script type="text/javascript">
function myFunction(id) {
   var txt = document.getElementById(id);
   txt.value = txt.value.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
}

function AddComma(text) {
    switch (text.value.length) {
       case 1:
           document.getElementById(text.id).value = "0.0" + text.value;
           break;
       default:
           var data = text.value.replace(".", "");
           var first = data.substring(0, (data.length - 2));
           var second = data.substring(data.length - 2);
           var temp = Math.abs(first) + "." + second;
           document.getElementById(text.id).value = temp;
     }
}
</script>

 

<div class="col-md-6">
     <div class="form-group">
         <label>Company Name<i class="text-red" style="font-size: large">*</i></label>
         <asp:TextBox ID="txt_company_name" runat="server" TabIndex="1" CssClass="form-control" onkeypress="return onlyAlphabets(event, this) && myFunction(this.id)" placeholder="Company Name" MaxLength="20"></asp:TextBox>
         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_company_name" ErrorMessage="Company Name Required" ForeColor="Red" TabIndex="1" SetFocusOnError="true" CssClass="pull-right" Font-Bold="true"></asp:RequiredFieldValidator>
     </div>
</div>

 The above javascript functions are used in many pages in my project so i wanted to minimise the code, I wrote a seperate javascript file and taking reference of that file as shown below.

<script src="Scripts/Functions.js" type="text/javascript"></script>

Problem is some functions are working like "onlyAlphabets(event, this)". you may see in textbox tag but these above functions aren't working...

Please help me...

You are viewing reply posted by: AnandM on Mar 14, 2017 04:04 AM.
Posted on Mar 14, 2017 04:04 AM Modified on on Mar 14, 2017 04:05 AM

Hi YacharuP,

I have created a sample which full fill your requirement you need to modify the code according to your need.

Function.js

function AddComma(element) {
    switch (element.value.length) {
        case 1:
            element.value = "0.0" + element.value;
            break;
        default:
            var data = element.value.replace(".", "");
            var first = data.substring(0, (data.length - 2));
            var second = data.substring(data.length - 2);
            var temp = Math.abs(first) + "." + second;
            element.value = temp;
    }
}

Default.aspx

<div>
    <script src="Function.js" type="text/javascript"></script>
    <h4>
        Page 1</h4>
    <br />
    <asp:TextBox ID="txtDecimalAmount" onkeyup="AddComma(this);" runat="server" />
</div>

Default2.aspx

<div>
    <script src="Function.js" type="text/javascript"></script>
    <h4>
        Page 2</h4>
    <asp:TextBox ID="txtPrice" onkeyup="AddComma(this);" runat="server" />
</div>

Default3.aspx

<div>
    <script src="Function.js" type="text/javascript"></script>
    <h4>
        Page 3</h4>
    <br />
    <asp:TextBox ID="txtAmount" onkeyup="AddComma(this);" runat="server" />
</div>

ScreenShot