Call External Javascript File in aspx file
-
Hi, I have written the javascript function in the External js file and named it as JScript.js. This file is in Javascripts folder which under the Project folder. The JScript.js file contains the following code. // JScript File function validateParameter() { var paramname=document.getElementById('<%=txtParam.ClientID %>'); var message; message=""; if(paramname.value=="") { message+="\n*Parameter Name is mandatory"; } else if(paramname.value!="") { var alphaExp = /^[a-zA-Z\s]+$/; if(!paramname.value.match(alphaExp)) { message+="\n*Only Alphabets are allowed for Parameter Name"; } } if(message!="") { alert(message); return false; } return true; } I have called the javascript file using the below html tag. I have called the javascript function for OnClientClick of a button. OnClientClick="javascript: return validateParameter();" But during the runtime the button click is not validating the controls. Please Help Me! Thanks.
-
Hi, I have written the javascript function in the External js file and named it as JScript.js. This file is in Javascripts folder which under the Project folder. The JScript.js file contains the following code. // JScript File function validateParameter() { var paramname=document.getElementById('<%=txtParam.ClientID %>'); var message; message=""; if(paramname.value=="") { message+="\n*Parameter Name is mandatory"; } else if(paramname.value!="") { var alphaExp = /^[a-zA-Z\s]+$/; if(!paramname.value.match(alphaExp)) { message+="\n*Only Alphabets are allowed for Parameter Name"; } } if(message!="") { alert(message); return false; } return true; } I have called the javascript file using the below html tag. I have called the javascript function for OnClientClick of a button. OnClientClick="javascript: return validateParameter();" But during the runtime the button click is not validating the controls. Please Help Me! Thanks.
Best way is to install Firebug or use the Firefox error console to know what's wrong with your JS.
anandhakrishnan wrote:
var paramname=document.getElementById('<%=txtParam.ClientID %>');
I am not sure, but I think the client id used in external JS will not get rendered. Have you checked? If it is not rendering, just pass the id as function parameter. Something like
function validateParameter(textBoxId)
{
var paramname = document.getElementById(textBoxId);
....
}
OnClientClick="javascript: return validateParameter('<%=txtParam.ClientID %>');"Navaneeth How to use google | Ask smart questions
-
Hi, I have written the javascript function in the External js file and named it as JScript.js. This file is in Javascripts folder which under the Project folder. The JScript.js file contains the following code. // JScript File function validateParameter() { var paramname=document.getElementById('<%=txtParam.ClientID %>'); var message; message=""; if(paramname.value=="") { message+="\n*Parameter Name is mandatory"; } else if(paramname.value!="") { var alphaExp = /^[a-zA-Z\s]+$/; if(!paramname.value.match(alphaExp)) { message+="\n*Only Alphabets are allowed for Parameter Name"; } } if(message!="") { alert(message); return false; } return true; } I have called the javascript file using the below html tag. I have called the javascript function for OnClientClick of a button. OnClientClick="javascript: return validateParameter();" But during the runtime the button click is not validating the controls. Please Help Me! Thanks.
anandhakrishnan wrote:
I have called the javascript file using the below html tag. I have called the javascript function for OnClientClick of a button. OnClientClick="javascript: return validateParameter();"
First try with alert on that method and check weather its called or not. Then You should pass the clientID also to the ValidateParameter().
anandhakrishnan wrote:
OnClientClick="javascript: return validateParameter();"
OnClientClick="javascript: return validateParameter(ClientID);"
cheers, Abhijit CodeProject MVP My Recent Article : Exploring Session in ASP.Net