Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 23:08pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 23-Dec-12 5:06am
   
after this step,how to apply validation of function to textbox.
ProgramFOX at 23-Dec-12 8:21am
   
Then, you can call your JavaScript function in your external file in your HTML file:

<script type="text/javascript">
functionInExternalFile();
</script>
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

>> 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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
0 DamithSL 300
1 Sergey Alexandrovich Kryukov 287
2 OriginalGriff 195
3 Zoltán Zörgő 149
4 Peter Leow 115
0 OriginalGriff 7,510
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 5,044
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,514


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100