Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
how to use javascipt function from .js file and i want to apply this function to textbox.
 
Ex:I have to do like this...

my textbox not allow numeric values.To do this I have make fuction.This function is in .js file.Now I have to use this function for textbox. I don't know javascript well.so any one can help me..
Posted 22 Dec '12 - 22:08


3 solutions

Hi,
 
In your HTML code, add this:
<script type="text/javascript" src="yourJavascriptFile.js"></script>
Now, your external JavaScript is loaded, and you can call your function.
 
Hope this helps.
  Permalink  
Comments
Member 9511889 - 23 Dec '12 - 5:06
after this step,how to apply validation of function to textbox.
ProgramFOX - 23 Dec '12 - 8:21
Then, you can call your JavaScript function in your external file in your HTML file: <script type="text/javascript"> functionInExternalFile(); </script>
>> First of all load the js file like
 
<script language="javascript" type="text/javascript" src="jsfilelocation/filename.js" />
 
>> Remeber to return true / false based on the condition in your validation function.
For example, in your js file, let's say there is a method called Validate() that validates the textbox. SO the function should return true / false
 
function Validate()
{
   // Validation code.
   // If numeric return true else return false;
...
}
 
>> Now in your submit button write...
<asp:Button ID="btnSave" runat="server" CssClass="ActionButton" Text="Save" ValidationGroup="vgAddress"
               OnClick="btnSave_Click" OnClientClick="javascript:return Validate();" 
  Permalink  
Try adding this function
 
function isAlphabet(elem){
    var alphaExp = /^[a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
        return true;
    }else{
        alert("Can Not Contain Number");
        elem.focus();
        return false;
    }
}
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 239
1 OriginalGriff 173
2 Maciej Los 135
3 Mahesh Bailwal 104
4 Aarti Meswania 98
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,917
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,125


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 23 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid