?
Solved

How to validate textboxes and radio buttons.

Posted on 2012-03-23
7
Medium Priority
?
275 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 1000 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 1000 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
Industry Leaders: 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 500 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 500 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

Technology Partners: 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!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

765 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