How to validate asp.net page by using javascript [Resolved]

Posted by Mahesh_Chs under JavaScript on 3/12/2012 | Points: 10 | Views : 25681 | Status : [Member] | Replies : 15
I want to validate my asp.net page having the controls like textboxs,chekboxes , dropdownlist,etc by using JavaScript So could you please send give me some suggestions..

Thank You




Responses

Posted by: Denny on: 3/13/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
You can use the validation controls like RequiredFieldValidator,RegularExpressionValidator etc from your Validation control ToolBox or you can directly write scripts in javascript as your question does not clearly specify what kind of data are to be validated

Refer this page

http://www.tizag.com/javascriptT/javascriptform.php

Regards,
Denny Kurian

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

Posted by: Patel28rajendra on: 3/13/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi, Mahesh

you can write javascript for this as follow


<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementById("<%=txtname.ClientID %>").value=="")
{
alert("Please Enter Name");
document.getElementById("<%=txtname.ClientID %>").focus();
return false;
}
if(document.getElementById("<%=ddlgender.ClientID%>").value=="0")
{
alert("Please select Gender");
return false;
}

}
</script>



and add textbox,dropdownlist and button

<asp:TextBox ID="txtname" runat="server"></asp:TextBox>



<asp:DropDownList ID="ddlgender" runat="server" Width="90px">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="2">Female</asp:ListItem>
</asp:DropDownList>


<asp:Button ID="btnsubmit" runat="server" OnClientClick="return validate();" Text="Submit" OnClick="btnsubmit_Click" />


here
document.getElementById("<%= ID of control.ClientID %>").value

this will get the value of the particular control
and you can check for different condition as per requirement


Thanks

Regards,

Rajendra Patel

R D Patel

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

Posted by: Hmanjarawala on: 3/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,
develope javascripts for validating controls on page.
use Custom Validater to validate perticular controls.
print message in Validation Summary control

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

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

Posted by: Sksamantaray on: 3/13/2012 [Member] Silver | Points: 25

Up
0
Down

You can go ahead with the suggestions given by Himanshu and Rajendra.
Additionally you can use validation controls and ajax toolkit controls.

Thanks,
Sanjay

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

Posted by: Mahesh_Chs on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
Thank U

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

Posted by: Mahesh_Chs on: 3/25/2012 [Member] Starter | Points: 25

Up
0
Down
Thank You it is working,
Could you please give me some more depth of this, like if i want to enter Only Letters or Only digits , and length of the string should be in between 6 and 8 characters and email validations.

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

Posted by: Patel28rajendra on: 3/26/2012 [Member] Starter | Points: 25

Up
0
Down
Hi


http://www.dotnetfunda.com/forums/thread8693-validation-of-java-scipt.aspx?com=added
you should refer link given in this post

R D Patel

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

Posted by: Sksamantaray on: 3/26/2012 [Member] Silver | Points: 25

Up
0
Down
good one

Thanks,
Sanjay

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

Posted by: Mahesh_Chs on: 3/27/2012 [Member] Starter | Points: 25

Up
0
Down
Hii

If i am writing Response.Redirect to so other page in onclick() event, it is redirecting to that page ,even the page is not validate

Thank You,
Mahesh

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

Posted by: Patel28rajendra on: 3/27/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

you have used validation control or javascript?


R D Patel

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

Posted by: Patel28rajendra on: 3/27/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

If you have use java script function

then on button 's client click return that function

for ex. I have used validate function
so write

OnClientClick="return validate();"


for that button

thanks

R D Patel

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

Posted by: Sabarimahesh on: 3/28/2012 [Member] Bronze | Points: 25

Up
0
Down
Patel28rajendra

Nice Man

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

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

Posted by: Mahesh_Chs on: 3/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Could you please tell me ,what is the error in this code

<script type="text/javascript">
function validateEmpty(fld) {
var error = "";
if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "The required field has not been filled in.\n"
alert(error);
}
else
{
fld.style.background = 'White';
}
return error;
}
</script>

In the Form

<asp:TextBox ID="txtid" runat="server" ></asp:TextBox>
<asp:Button ID="btnsubmit" runat="server" Text="Submit"
OnClientClick="return validateEmpty(txtid)" onclick="btnsubmit_Click" />

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

Posted by: Sabarimahesh on: 3/29/2012 [Member] Bronze | Points: 25

Up
0
Down
Mahesh_Chs

Wat is ur Error can you sent me..

An change it return validateEmpty('txtid')

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

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

Posted by: Patel28rajendra on: 3/29/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

Just modify return statement as follow

return false;

instead of

return error;

Thanks

R D Patel

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

Login to post response