Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have button "btnSave" in my aspx page. in its click event it execute save() method. I want to show alert if any textbox "tstOperation" remain empty. I have to use jquery to show this alert. Please help me.
Posted

C#
function check() {
    var textBox =  $.trim( $('#tstOperation').val() )
    if (textBox == "") {
        alert('Please enter text');
       return false;
    }
}


Call this method on your save button's "onclientclick" event.
onclientclick="return check();"
 
Share this answer
 
v2
Hi,

Its seems like that you are going to implement js valiadtion on your form using jQuery. To achive goal we follows few steps such as

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="TestDemo._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<asp:TextBox ID="txtOperation" runat="server">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return save()" />

<script type="text/javascript">
function save() {
var operationValue = $("#<%=txtOperation.ClientID %>").val();
if (operationValue.trim() == '') {
alert("operation value is empty");
return false;
}
return true;
}
</script>
 
Share this answer
 
v4
Comments
Sumon562 1-Sep-14 0:16am    
I have used master page. But I use this javascript method in content page. your above mentioned function does not work. Content page does not have <form> tag.
Sandeep Singh Shekhawat 1-Sep-14 0:29am    
I have updated source code as per your requirment please have a look with new code. Its inherit master page and its a content page.
Sandeep Singh Shekhawat 1-Sep-14 0:19am    
Its no matter, you can write this method on content page as well as seprate js file and that file add to content page.
Ashish Wazarkar 7-Sep-16 8:52am    
ok

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