Click here to Skip to main content
15,910,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!
I am new to asp.net. I am working on a form containing some textboxes for userid, password and some drop down lists. I have to do some validation using java script. I have written some code as follows...
JavaScript
<script type="text/javascript">
function validate() {
if (document.getElementById("<%=txtuid.ClientID%>").value=="")
      {
      
                 alert("id Field can not be blank");
                 document.getElementById("<%=txtuid.ClientID%>").focus();
                 return false;
      }
}
</script>


XML
<body>
<form id="someID">
 <asp:textbox id="txtuid" />
<asp:button id="btn" onclientclick="return validate()" />
</form>
</body>


but the problem is that when I am placing it on a separate javascript file then its not working at all...

XML
<head>
  <script src="script/JScript.js" type="text/javascript"></script>
</head>
<body>
<form>
  <asp:button id="btnsubmit" runat="server" text="Next" width="122px" onclick="btnsubmit_Click" onclientclick="return validate()"/>
  </asp:button>
</form...>
</body>


Please help!
Posted
Updated 2-Jul-10 3:25am
v3
Comments
Ankur\m/ 2-Jul-10 9:25am    
Edited for formatting and readability.
Chris Maunder 3-Jul-10 9:39am    
ASP.NET validation controls emit client side validation. Did you read the article link I posted?

The JavaScript file does not generate on the server, but the page does. The reason the code doesn't work is that this <%=txtuid.ClientID%>" never gets evaluated.

What you might do is make the validation script more generic and pass in the control and text to display.

In the script:
JavaScript
function validate(control, text) {
if (control.value=="")
   alert(text);


And in the page:

XML
<asp:button id="btnsubmit" runat="server" text="Next" width="122px" onclick="btnsubmit_Click" onclientclick="return validate(this, 'some message');" />
 
Share this answer
 
v3
Comments
i.ahmad86 2-Jul-10 13:07pm    
but i dont think i can use it foer dropdown and other controls...
Why not use the ASP.NET validation controls?

Validator Controls in ASP.NET[^]
 
Share this answer
 
Comments
i.ahmad86 3-Jul-10 0:37am    
because i need only client side validations.. not on server side...
Use the following code

in your head tag type the following function
<script language="Javascript" type="text/javascript">
function validateME()
{
    if(document.getElementById("txtuid").value=="")
       {
          alert("Please enter user id");
          document.getElementById.focus();
          return false;
       }
}
</script>


I observed that your textbox has no runat="server" property

do the following
<body>
        <form id="frmtest">
           <asp:textbox id="txtuid" runat="server" xmlns:asp="#unknown" />
           <asp:button id="btnPressMe" onclientclick="return validateME();" runat="server" xmlns:asp="#unknown" />
        </form>
</body>


hope this helps you...:thumbsup:
 
Share this answer
 
v2
Comments
i.ahmad86 3-Jul-10 2:28am    
sorry its not working.... pls help..
i.ahmad86 4-Jul-10 1:18am    
the script u specify is correct but it will work only on the aspx page. but the scenario is to use the validation on the separate .js file. Its not working in that way... :(
The following is in Validation.js file:

JavaScript
function validateME(controlID, textMessage)
{
    if(document.getElementById(controlID).value == "")
    {
          alert(textMessage);
          document.getElementById.focus();
          return false;
    }
}


In header section of html include script tag:
<script language="text/javascript" src="./pathTo/Validation.js"><script>


In your ASP.NET page do this:

ASP
<asp:button id="btnsubmit" runat="server" text="Next" width="122px" onclick="btnsubmit_Click" onclientclick="return validate()"/>


Now in your code behind file in page load somewhere do this:
C#
btnsubmit.OnClientClick = String.Format("return validateME('{0}','{1}');",btnsubmit.ClientID, "Field Xxx may not be emtpy");


By setting the OnClientClick from the behind code allows you to send all the nescessary parameters along that are needed.


If you have problems realizing this solution your welcome to ask me.


Cheers


Manfred
 
Share this answer
 
v3
Hi,

<%=txtuid.ClientID%> which is not working in javascript...
One solution is that you should find the client id from the aspx page load function and return this client id into javascript function.

Then you can find the control by adding this client id before controlid.

I'll give you one example:

in the pageload
Page.RegisterStartupScript("Pageload","<script>Users.PageLoad('" + this.ClientID + "_');</script>");

in the java script
Users = { 
PageLoad: function (id) {
    ClientId = id;
  
},



Hope this may help you
 
Share this answer
 
v4

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