Solved

How to validate textboxes and radio buttons.

Posted on 2012-03-23
7
263 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
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now