Solved

How to validate textboxes and radio buttons.

Posted on 2012-03-23
7
273 Views
Last Modified: 2012-03-26
1. I have 2 radio button rbmale and rbfemale. How to validate or make 1 cumpulsory.
2. I have a 2 texboxes txtmobile and txtname.
txtmobile to be validated for numeric entry.
txtname to be validated for text entry.
0
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Assisted Solution

by:Rose Babu
Rose Babu earned 250 total points
ID: 37756385
Hi,

To make a radiobutton as cumpulsory, you need to set one group name for the radiobuttons. and you need to set the checked property to true for default selection

and do the textBox validations in client side and server side are as coded below. Have a look and implement in your project.
<script language="javascript" type="text/javascript">
	function CheckVal() {

		var txtmobile = document.getElementById("<%=txtmobile.ClientID %>");
		var txtname = document.getElementById("<%=txtname.ClientID %>");

		// Mobile Number Validation
		if (txtmobile.value == "") {
			alert("Mobile number should not be empty!");
			txtmobile.focus();
			return false;
		}
		else if (isNaN(txtmobile.value)) {
			alert("Mobile number should be a number!");
			txtmobile.focus();
			return false;
		}

		// Name validation
		if (txtname.value == "") {
			alert("Name should not be empty!");
			txtname.focus();
			return false;
		}
	}
</script>


<table>
<tr>
	<td colspan="2">
		<asp:Label ID="lblValErr" runat="server" Text=""></asp:Label>
	</td>
</tr>
<tr>
	<td colspan="2">
		<asp:RadioButton ID="rbmale" runat="server" GroupName="rbtnGroup1" Checked="true" Text="Male" />
		&nbsp;&nbsp;
		<asp:RadioButton ID="rbfemale" runat="server" GroupName="rbtnGroup1" Text="Female" />
	</td>
</tr>
<tr>
	<td>
		Mobile:
	</td>
	<td>
		<asp:TextBox ID="txtmobile" runat="server"></asp:TextBox>
	</td>
</tr>
<tr>
	<td>
		Name:
	</td>
	<td>
		<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
	</td>
</tr>
<tr>
	<td>
		<input id="btnClientVal" type="submit" value="Client Validation" onclick="javascript:return CheckVal();" />
	</td>
	<td>
		<asp:Button ID="btnServerVal" runat="server" Text="Server Validation" />
	</td>
</tr>
</table>

Protected Sub btnServerVal_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnServerVal.Click

	lblValErr.Text = ""
	Dim Err As Boolean = False

	If txtmobile.Text.Trim = "" Then
		lblValErr.Text = "Mobile number should not be empty!<br />"
		Err = True
	ElseIf Not IsNumeric(txtmobile.Text.Trim) Then
		lblValErr.Text += "Mobile number should be a number!<br />"
		Err = True
	End If

	If txtname.Text.Trim = "" Then
		lblValErr.Text += "Name should not be empty!<br />"
		Err = True
	End If

	If Err = True Then
		Exit Sub
	End If

	' Successful validation and do code further

End Sub

Open in new window

Try this. It may help you.
0
 

Author Comment

by:searchsanjaysharma
ID: 37756507
I am developing windows based application.
secondly i have kept 2 radiobutton in groupbox. so no need to give groupname.
Hence please give the solution
0
 
LVL 16

Assisted Solution

by:Rose Babu
Rose Babu earned 250 total points
ID: 37756571
Good. but you should have mentioned it in the question. Ok.

as you said, put those two radio button inside a group box. and set the checked property to true for any one radio button.

and the same validation condition code in my previous comment will work in window application also.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

	If txtmobile.Text.Trim = "" Then
		MsgBox("Mobile number should not be empty!")
		Exit Sub
	ElseIf Not IsNumeric(txtmobile.Text.Trim) Then
		MsgBox("Mobile number should be a number!")
		Exit Sub
	End If

	If txtname.Text.Trim = "" Then
		MsgBox("Name should not be empty!")
		Exit Sub
	End If

End Sub

Open in new window

Try this.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
nepaluz earned 125 total points
ID: 37756980
1. For numeric textbox, add this function
Private Function TrapKey(ByVal KCode As String) As Boolean
    If (CDbl(KCode) >= 48 And CDbl(KCode) <= 57) Or {8, 46, 44, 127, 45}.Contains(CInt(KCode)) Then
        TrapKey = False
    Else
        TrapKey = True
    End If
End Function

Open in new window

The add this to the KeyPress event of the textbox
e.Handled = TrapKey(CStr(Asc(e.KeyChar)))

Open in new window

2. For the text validated textbox, add this to the KeyPress event of the textbox
e.Handled = If(IsNumeric(e.KeyChar), False, True)

Open in new window

0
 

Author Comment

by:searchsanjaysharma
ID: 37764508
Giving error

If ((CDbl(KCode) >= 48 And CDbl(KCode) <= 57) Or (CDbl(KCode) >= 48 And CDbl(KCode) <= 8) Or (CDbl(KCode) >= 48 And CDbl(KCode) <= 46) Or (CDbl(KCode) >= 48 And CDbl(KCode) <= 44) Or (CDbl(KCode) >= 48 And CDbl(KCode) <= 127) Or (CDbl(KCode) >= 48 And CDbl(KCode) <= 45)).Contains(CInt(KCode)) Then
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 125 total points
ID: 37765133
For radio buttons, use a condition like

If rbMale.Checked = false And rbFemale.Checked = false then
   msgbox "Please specify gender"
End If
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37767058
ok
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question