Client-side validation of controls

G

Guest

Hi all,

I have a situation in which I need to verify that either of two textboxes
contain an entry; I'm initially uninterested in whether or not the entries
are valid, only that there is at least one entry. The validity of the entry
will be checked when I first post back the data to the server, but I don't
want to perform a postback if both textboxes are empty.

My initial thought is to write a simple javascript function to return true
if there's an entry in either box or return false if both boxes are empty. I
could then call this function from another javascript function that would
intercept the onclick event for my form's pushbutton. The problem I'm
encountering is I can't get a javascript function to work for the button's
onclick event. I created a function called ButtonClicked and have it wired
up the the button's onclick event by stating 'onclick="ButtonClicked"' in the
html code for the pushbutton, but I don't know where or how to obtain the
Object and the EventArgs my code-behind handler expects for the pushbutton.

Does anyone have a brief example or can anyone point me where I can find one?

Thanks!
 
M

Mark Rae

Does anyone have a brief example or can anyone point me where I can find
one?

<script type="text/javascript">
function validateForm()
{
if
(document.getElementById('<%=txtTextBox1.ClientID%>').value.length == 0
&& document.getElementById('<%=txtTextBox2.ClientID%>').value.length
== 0)
{
alert('Both textboxes cannot be blank');
return false;
}
}
</script>

<form id="frmDefault" runat="server">
<asp:TextBox ID="txtTextBox1" runat="server" /><br />
<asp:TextBox ID="txtTextBox2" runat="server" /><br />
<asp:Button ID="cmdSave" runat="server" Text="Save"
OnClick="cmdSave_Click" OnClientClick="return validateForm();" />
</form>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top