Solved

How to validate textboxes and radio buttons.

Posted on 2012-03-23
7
266 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Showdialog 8 29
Help with WinAPI Signatures in VB.NET for SetWindowSubclass 2 35
VB.Net How to Exit Sub - Exit Form??? 5 51
Advice in Xamarin 21 57
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

23 Experts available now in Live!

Get 1:1 Help Now