Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Frnds,

I have created a web page in asp.net using c# Sqlserver 2005.

In this i have textboxes and a submit button to save the data in database.
But here, i need to set validations here
I have 3 fields as Telephone Home, Telephone Office, MobileNo
Here User can enter in enter in 3 textboxes
user can enter in 2 textboxes,
but 3 textboxes should not be empty, at least one should filled

Please can you help me, how to do this.
Thanks in advance
Posted
Comments
Kornfeld Eliyahu Peter 11-Mar-14 7:23am    
Do some reading here - http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx

hello i have created simple code ,u can try this -

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
          <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>



c# code -

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text == "")
        {
            Label1.Text = "mobile number does not save";

        }
        else
        {

            Label1.Text = "save ur mobile number ";


        }
    }
}


if all text box null than it show mobile number does not save otherwise if any textbox have some value than mobile number will be successfully saved.
 
Share this answer
 
v2
Hi,
Here i did with java script working fine.Check this add the bellow script in head section
JavaScript
<script type="text/javascript">
    <!--
    function validation()
    {
            if (document.getElementById("txtPhoneHome").value == "" && document.getElementById("txtPhoneOffice").value == ""&&document.getElementById("txtMobile").value == "")
            {
                alert('Please enter atleast one contact no.');
                document.getElementById("txtMobile").focus();
                return false;
            }

        }
    -->
    </script>


and here html form

ASP.NET
<asp:textbox id="txtPhoneHome" runat="server" onkeypress="return clickButton(event,'btnSubmit')"></asp:textbox>
        <asp:textbox id="txtPhoneOffice" runat="server" onkeypress="return clickButton(event,'btnSubmit')"></asp:textbox>
        <asp:textbox id="txtMobile" runat="server" onkeypress="return clickButton(event,'btnSubmit')"></asp:textbox>

        <asp:button id="btnSubmit" runat="server" text="Submit" onclientclick="return validation();" />
 
Share this answer
 
v2
use custom validation funuction and use this property

ClientValidationFunction="Validate"


JavaScript
function Validate(sender, args) {
       var txt1 = document.getElementById("txtphoneh");
       var txt2 = document.getElementById("txtphonew");
       var txt3 = document.getElementById("txtphonec");
       args.IsValid = ((txt1.value != "") || (txt2.value != "") || (txt3.value != ""));

           }
 
Share this answer
 
Client side Validation is better.....

XML
function ValidateData(){

      var TextVal= document.getElementById('<%=txtbox1.ClientID %>').value;
          if (TextVal== "") {
              alert("text box Can't be empty ");
              document.getElementById('<%=txtbox1.ClientID%>').focus();
              return false;
          }

          var textpart = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
          var validtext = textpart .test(TextVal);

          if (!validtext ) {
              alert("Your textbox contain invalid ..... again.");
              document.getElementById('<%=txtEmailID.ClientID%>').focus();
              return false;}
}
else
return true



call ValidateData() in clientclik event ......
 
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