Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Try adding this function

C#
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;
    }
}
 
Share this answer
 
>> First of all load the js file like

JavaScript
<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

JavaScript
function Validate()
{
   // Validation code.
   // If numeric return true else return false;
...
}


>> Now in your submit button write...
VB
<asp:Button ID="btnSave" runat="server" CssClass="ActionButton" Text="Save" ValidationGroup="vgAddress"
               OnClick="btnSave_Click" OnClientClick="javascript:return Validate();" 
 
Share this answer
 
Hi,

In your HTML code, add this:
HTML
<script type="text/javascript" src="yourJavascriptFile.js"></script>

Now, your external JavaScript is loaded, and you can call your function.

Hope this helps.
 
Share this answer
 
Comments
Member 9511889 23-Dec-12 5:06am    
after this step,how to apply validation of function to textbox.
Thomas Daniels 23-Dec-12 8:21am    
Then, you can call your JavaScript function in your external file in your HTML file:
<small>
<script type="text/javascript">
functionInExternalFile();
</script></small>

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