Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI experts,

I have a registration webpage, In this webpage I have 3 textboxes and a submit button.
am using Sqlserver 2005 database for inserting these values in database.
These values are storing Successfully when i click on Submit button.

Till here, it is working fine.

now my requirement is,

If i click on submit and the textboxes are empty, it must display a message please enter the required fields.

if the textboxes are filled with data then, when i click the submit button this should be displayed.


"Are you sure you want to Submit this data ?"


if Successfully stored in DB, Display a message....Successfully Submitted.

Please help me, am in confuse, thanks.
Posted
Comments
Dholakiya Ankit 11-Jul-13 6:19am    
both solutions below are perfect and you can also do this at c# also

Try this

ASP.NET
<head>
<script type="text/javascript">
        function Validate() {
            var txt1 = document.getElementById('TextBox1');
            var txt2 = document.getElementById('TextBox2');
            var txt3 = document.getElementById('TextBox3');
            
            if(txt1.value == '' || txt2.value == '' || txt3.value == '')
            {
                alert('please enter value');
                return false;
            }
            else
            {
                return confirm('Do you want to continue')
            }
        }
    </script>
</head>
<body>
<asp:textbox runat="server" txt="valid" id="TextBox1" xmlns:asp="#unknown"></asp:textbox>
      <asp:textbox runat="server" txt="valid" id="TextBox2" xmlns:asp="#unknown"></asp:textbox>
       <asp:textbox runat="server" txt="valid" id="TextBox3" xmlns:asp="#unknown"></asp:textbox>
    <asp:button runat="server" id="btnSubmit" text="save" xmlns:asp="#unknown">
        OnClientClick="javascript:return Validate()" onclick="btnSubmit_Click" />/<body></body></asp:button></body>
 
Share this answer
 
Comments
Member239258 11-Jul-13 6:52am    
Thnak you sumit_kapadia
Code for blank field validations:
C#
<asp:textbox id="TextBox1" value="" xmlns:asp="#unknown"></asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" errormessage="" forecolor="red" controltovalidate="TextBox1" validationgroup="A" initialvalue="" xmlns:asp="#unknown">*</asp:requiredfieldvalidator>

<asp:textbox id="TextBox2" textmode="Password" value="" xmlns:asp="#unknown"></asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator4" runat="server" errormessage="" forecolor="red" controltovalidate="TextBox2" validationgroup="A" initialvalue="" xmlns:asp="#unknown">*</asp:requiredfieldvalidator>

<asp:button id="btn_submit" runat="server" text="Submit" onclick="btn_submit_Click" onclientclick="return confirm('Are you sure you want to enter details?');" validationgroup="A" xmlns:asp="#unknown" />


Code for entering details:
C#
protected void btn_submit_Click(object sender, EventArgs e)
{
//You code here
Response.Write("<script type='text/javascript'>");
Response.Write("alert('Your details has been submitted successfully.');");
Response.Write("</script>");
}

Regards.. :laugh:
 
Share this answer
 
v3
 
Share this answer
 
Comments
santhiyaa 11-Jul-13 7:17am    
simple and nice
reefer for validations..:)

http://www.tutorialspoint.com/asp.net/asp.net_validators.htm[^]

How to Use the ASP.NET Validation Control to Validate the User Input[^]

For Confirm boxes...:)

C#
 <script type="text/javascript">
        function confirmation() {
            if (confirm('are you sure you want to Submit Data?')) {
            return true;
            }else{
            return false;
            }
        }
    </script> 

<asp:button id="Button1" runat="server" text="Button" xmlns:asp="#unknown">
        OnClientClick="return confirmation();" onclick="Button1_Click"/></asp:button>



After submited Data paste this code at Code behind..:)

C#
Page.ClientScript.RegisterClientScriptBlock(
   this, 
   GetType(), 
   "ALERT", 
   "alert('Data has been Submited in Database')", 
   true);
 
Share this answer
 
Hi,

you can add "Required field Validator " to your text boxes..

set controltovalidate property to textboxid
and error msg you can set as per your requirement.

for eg.

<asp:TextBox runat="server" id="txtName" />
    <asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="txtName" errormessage="Please enter your name!" />


and about your second question..
see the bolow link...

http://forums.asp.net/t/1385869.aspx[^]
 
Share this answer
 
v2
//asp validation to check for empty fields.
XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvTextBox1" runat="Server" ValidationGroup="Submit"
            ControlToValidate="TextBox1" ErrorMessage="Cannt be empty"></asp:RequiredFieldValidator>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvTextBox2" runat="Server" ValidationGroup="Submit"
            ControlToValidate="TextBox2" ErrorMessage="Cannt be empty"></asp:RequiredFieldValidator>
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvTextBox3" runat="Server" ValidationGroup="Submit"
            ControlToValidate="TextBox3" ErrorMessage="Cannt be empty"></asp:RequiredFieldValidator>
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit" Onclick="btnSubmit_Click" OnClientClick="return fnConfirm();" />



//Java script function
function fnConfirm()
{
C#
var result=confirm('Are you sure you want to Submit this data ?');
 if (result==true) 
        {                  
           return true;
        }
        else
        {
            return false;
        }
}



Note : If user click on ok system will allow to go for server side click event otherwise no.



//Confirmation message after saving successfully
if (result is true)
{
ScriptManager.RegisterClientScriptBlock(Me.Page,Me.GetType(),"Saved","alert('Records have been saved succesfully')",True);
return;
}
else
{
C#
ScriptManager.RegisterClientScriptBlock(Me.Page,Me.GetType(),"Saved","alert('Failed to save try again')",True);
return;


}
 
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