Calling JavaScript Client Side Validation function and Server side function

Posted by Sidharthgs6 under ASP.NET on 9/5/2012 | Points: 10 | Views : 3318 | Status : [Member] | Replies : 7
Hi,

How to calling javascript client side function while clicking asp.net button control.

If client side valid only means then only onclick event function called

otherwise intimate to client error

Give me code or any good link for this




Responses

Posted by: Maheshvishnu on: 9/5/2012 [Member] Starter | Points: 25

Up
0
Down
Put your javasript code into a function,then pass that funtion to the event of your respective control



mahesh

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

Posted by: Sidharthgs6 on: 9/5/2012 [Member] Starter | Points: 25

Up
0
Down
Give me example program I cant understand what will you say.

so give some sample program

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

Posted by: Vasanthmvp on: 9/5/2012 [Member] Starter | Points: 25

Up
0
Down
Hi, Here is an example:

Under <head> Part:

<script type="text/javascript" language="javascript">
function Method() {
if (document.getElementById("txt1").value == '') {
document.getElementById("lbl").innerHTML = "Enter UserName";
return false;
}
}
</script>

.aspx Page:

UserName : <asp:TextBox ClientIDMode="Static" ID="txt1" runat="server" /><asp:Label ID="lbl" ClientIDMode="Static" runat="server" /> <br />
<asp:Button ID="btn1" runat="server" OnClientClick="return Method()" Text="Submit" />


Awesome Coding !! :)

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

Posted by: Hariinakoti on: 9/5/2012 [Member] Starter | Points: 25

Up
0
Down
Good Explanation Vasanth..

Thanks & Regards
Hari

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

Posted by: Sidharthgs6 on: 9/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

I understand above concept.

Now i have one problem that is where the javascript client code write in masterpage content form.

I write the code in following manner

<%@ Page Language="C#" MasterPageFile="~/CRM.master" AutoEventWireup="true" CodeFile="Complaint.aspx.cs" Inherits="Complaint" Title="Untitled Page" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<script type="text/javascript">
function ValidatePhone()
{
var txt_Phone=document.getElementById("txt_Phone").value;
var regularExpression= /^[0-9]{10,}/;
if (!txt_Phone.match(regularExpression)) {
alert("Please enter 10 digit mobile number");
return false;
}
else
return true;
}
</script>

<asp:TextBox ID="txt_Description" runat="server" Width="185px" Height="51px"
TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="Valid_Description" runat="server"
ControlToValidate="txt_Description" ErrorMessage="*"></asp:RequiredFieldValidator>

<asp:TextBox ID="txt_Phone" runat="server" Height="25px" Width="186px"></asp:TextBox>
<asp:RequiredFieldValidator ID="Valid_Phone" runat="server"
ControlToValidate="txt_Phone" ErrorMessage="*"></asp:RequiredFieldValidator>

<asp:Button ID="btn_Submit" runat="server" Text="Submit"
onclick="btn_Submit_Click" OnClientClick="return ValidatePhone() />

</asp:Content>

My problem is the javascript is not ckecking to validate.

Please Guide me what problem is the above code

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

Posted by: Vasanthmvp on: 9/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi, you include the java script code in to the master page and set clientIdmode = static to those asp controls that you are using in the java script. I think, now it works fine.
If the explanation suits ua problem, mark it as answer.

Regards,

Awesome Coding !! :)

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

Posted by: Sidharthgs6 on: 9/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

There is no clientIdmode property for asp.net textbox
where i set clientIdmode give me liitle sample code

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

Login to post response