Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I want to do client side validation of a form before posting it to the server. I've written the aspx code for the form but don't know how to do javascript validation with the same page.
The fields are:
1. name
2. roll
3. submit button
*********************************
<form   runat="server">
<table>
<tr>
<td>Name:</td>
<td><asp:TextBox ID="name" runat="server" /></td>
</tr>

<tr>
<td>Roll:</td>
<td><asp:TextBox ID="roll" runat="server" /></td>
</tr>

<tr>
<td><asp:ImageButton ID="ImgSubmit" runat="server" ImageUrl="~/images/submit.gif"/></td>
</tr>

</table>
</form>

***********************************
How to do javascript validation with this aspx form?? Also I want to include some features with javascript like:
1) when the page loads the cursor must be at the "Name" textbox
2) initially the submit button must be disabled
3) change the color of the text box when it is focused.

How to include java script code with this form??
Posted
Updated 23-Jun-11 19:02pm
v3

You just put the javascript on the form and it's rendered directly into your html. Then you can put event handlers etc in your controls, any thing not recognised by the framework is passed through verbatim.
 
Share this answer
 
Try this....
XML
<form   runat="server">
 <script type="text/javascript" language="javascript">
//put javascript here

<table>
<tr>
<td>Name:</td>
<td><asp:TextBox ID="name" runat="server" /></td>
</tr>
<tr>
<td>Roll:</td>
<td><asp:TextBox ID="roll" runat="server" /></td>
</tr>
<tr>
<td><asp:ImageButton ID="ImgSubmit" runat="server" ImageUrl="~/images/submit.gif"/></td>
</tr>
</table>
</form>

--------------
Vote if got an solution
thanks and reuards
kaushal16
 
Share this answer
 
Javascript is client side scripting mostly used for validation.

U can write a script under <HEAD> tag

Function to put cursor on textbox

function putFocus()
{
   document.getElementById("TextboxID").focus();
   return false;
}


call the function on Load of html Body tag

<body onload="return putFocus()"></body>
 
Share this answer
 
I've written script for checking the length of the name text but is not working.
***************************************
<script type="text/javascript" language="javascript">
function validate()
{
var txtname = form1.TxtName.value;
if (txtname.length<4)
{
alert("invalid username");
return false;
}
else return true;
}
</script>


<form id="form1" runat="server">
<table border="0">
<tr>
<td>Name</td>
<td><asp:TextBox ID="TxtName" onblur="validate()" runat="server"></asp:TextBox>
</td>
</tr>

<tr>
<td>Roll</td>
<td><asp:TextBox ID="TxtRoll" runat="server"></asp:TextBox>
</td>
</tr>


</table>
</form>
************************************
I am new in javascript. I can't figure out the error.How to check the length of the text entered??
How to write javascript for changing the border-color of the textbox when it is focused??
 
Share this answer
 

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