hello,

I have 3 textboxes and user should fill atleast 1 of those.
so i have to check that.
How to do it using Required field validator/javascript function.

Note: I am not using form , the text boxes are in content holder of the page. If i use javascript how to call it on submit button which has insert code also.

Create instances of RequiredField control dynamically.

Refere this sample code

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

<!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 id="Head1" runat="server">
    <title>Minimize IE</title>

    <script language="javascript">
        function validateTextBoxes() {

            //store trimmed values of textboxes into variables
            var txt1 = document.getElementById('TextBox1').value.replace(/^\s+|\s+$/g, '');
            var txt2 = document.getElementById('TextBox2').value.replace(/^\s+|\s+$/g, '');
            var txt3 = document.getElementById('TextBox3').value.replace(/^\s+|\s+$/g, '');

            if ((txt1 == null || txt1 == "") &&
                (txt2 == null || txt2 == "") &&
                (txt3 == null || txt3 == "")) {
                alert('Atlease one textbox should be entered');
                return false;
            }
            else
                return true;
        }
        
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    &nbsp;<asp:Button ID="btnSubmit" OnClientClick="return validateTextBoxes()" runat="server"
                        Text="Submit" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

try using dreamweaver validate form function. then use the code from the onclick or onblur whatever event is there and move it to the respective textbox. the validate form feature uses javascript.

following is my javascript code :

<script>
function Check(frm) 
{
var input, notEmpty; 
var div = document.getElementById("chk");
for(var i=0;i<div.childNodes.length;i++) 
{
input = div.childNodes[i];
if(input.type == "text") 
{
if(input.value != "") 
{
notEmpty = true;break; 
}
else
{
notEmpty = false; 
}
}
}
if(notEmpty == false) 
{
alert("Atleast one email id must be entered."); 
}
return notEmpty; 
}
</script>

and I am checking for server side validations like email also on the same button click.

So tell me how to check both and how to call this script.

my friend this logic is not gonna work.....

if i add two spaces....that will blow your logic...

may be u should try the logic suggested by Mr. Ramesh..

Hope it helps...

following is my javascript code :

<script>
function Check(frm) 
{
var input, notEmpty; 
var div = document.getElementById("chk");
for(var i=0;i<div.childNodes.length;i++) 
{
input = div.childNodes[i];
if(input.type == "text") 
{
if(input.value != "") 
{
notEmpty = true;break; 
}
else
{
notEmpty = false; 
}
}
}
if(notEmpty == false) 
{
alert("Atleast one email id must be entered."); 
}
return notEmpty; 
}
</script>

and I am checking for server side validations like email also on the same button click.

So tell me how to check both and how to call this script.

The problem is that it is executing the onclick code and insert the data , whether the textboxes are filled or not.

I used Page.IsValid condition also

I think the code inside the for loop and if condition is not executed.

Set the value of the notEmpty to false by default.

var input, notEmpty = false;

Also call this function in the OnClientClick of the button as below

OnClientClick="return Check(form1)"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.