Solved

How to validate textboxes and radio buttons.

Posted on 2012-03-23
7
271 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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