Validation in webpage [Resolved]

Posted by Sudhakar_A under ASP.NET on 9/6/2013 | Points: 10 | Views : 981 | Status : [Member] | Replies : 4
How to validate user controls in javascript




Responses

Posted by: Bandi on: 9/6/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
It's often needed to validate a user control in aspx page but document.getElementById('controlname') does not get us that usercontrol.
Suppose we create a user control that has only asp:text box named 'txt', and we have to validate it in our page.

<myText:Textbox ID="txt" runat="server" Visible="true"  />

<asp:Button ID="btnSumbit" runat="server" Text="Validate user control" OnClientClick="javascritp:return IsFilled()" />


You can use the following JavaScript to get that control.

var a=document.getElementById('form1').elements("txt$txt");

if(a.value=='')
{
alert("please enter value");
return false;
}


Refer this link:
http://www.aspforums.net/Threads/552423/Validate-TextBox-inside-UserControl-using-JavaScript-in-ASPNet/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/6/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Dear Sudhakar,

UserControl:

First Name: <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox><br />

Last Name: <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox><br />


Page:

<script type="text/javascript">

function Validate() {
var txtFirstName = document.getElementById('<%=UserControl1.FindControl("txtFirstName").ClientID %>');
var txtLastName = document.getElementById('<%=UserControl1.FindControl("txtLastName").ClientID %>');
if (txtFirstName.value == "") {
alert("Please enter First Name");
return false;
}
if (txtLastName.value == "") {
alert("Please enter Last Name");
return false;
}
}
</script>


<asp:Button ID="btnValidate" runat="server" Text="Validate" OnClientClick="return Validate()" />


Happy Coding.

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/6/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can also see the below link:-

http://www.codeproject.com/Tips/210206/How-to-validate-a-user-control-in-ASPX-page

Happy Coding.

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 9/8/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.javascript-coder.com/html-form/javascript-form-validation.phtml

If this post helps you mark it as answer
Thanks

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response