Click here to Skip to main content
12,763,796 members (38,364 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
Hi all,I have an issue...

I have 4 pair of textboxes ,each pair one below the other.

if user enters some values say 5 in textbox1 and 150 in textbox2.then he should not be able to enter the value between 5 and 150 in any of the textboxes,like wise validations on all text boxes.


2 70

80 150

75 90

is not allowed..

it should be

2 70

-100 1

80 150

71 79

is proper entry..

How can i do this..i know this is not a proper way to do this..Can anyone suggest a proper solution...thanks in advance
Posted 5-Oct-12 3:21am
Updated 5-Oct-12 3:22am
Michael Siroen 5-Oct-12 8:26am
In what technology stack are you referring?
Is this a web application, or WPF / WinForms?
If this is a web application, do you only refer to client side validation or do you want a complete server-side and client-side solution?
Also can you please show a piece of code what you already tried before (and perhaps why it didn't work as you expected)
DileepkumarReddy 5-Oct-12 8:33am
I am working on WinForms, a desktop application...
DileepkumarReddy 5-Oct-12 8:38am
I did something like this..

Private Sub xtbColor1From_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles xtbColor1From.LostFocus
'If (xtbColor1From.Text = "") Or (XtbColor2From.Text = "") Or (XtbColor3From.Text = "") Or (XtbColor4From.Text = "") Or (xtbColor1To.Text = "") Or (XtbColor2To.Text = "") Or (XtbColor3To.Text = "") Or (XtbColor3To.Text = "") Then
'Exit Sub
'End If
If xtbColor1From.Text.Length = 0 Then
End If
If XtbColor4From.Text <> "" AndAlso XtbColor4To.Text <> "" AndAlso XtbColor4From.Text <> "-" AndAlso XtbColor4To.Text <> "-" _
AndAlso XtbColor3From.Text <> "" AndAlso XtbColor3To.Text <> "" AndAlso XtbColor3From.Text <> "-" AndAlso XtbColor3To.Text <> "-" _
AndAlso XtbColor2From.Text <> "" AndAlso XtbColor2To.Text <> "" AndAlso XtbColor2From.Text <> "-" AndAlso XtbColor2To.Text <> "-" Then

If (CDbl(xtbColor1From.Text)) < (CDbl(XtbColor4To.Text)) AndAlso (CDbl(xtbColor1From.Text)) > (CDbl(XtbColor4From.Text)) Then
xtbColor1From.Text = String.Empty
MsgBox("Invalid Range")

End If
If (CDbl(xtbColor1From.Text)) < (CDbl(XtbColor2To.Text)) AndAlso (CDbl(xtbColor1From.Text)) > (CDbl(XtbColor2From.Text)) Then
xtbColor1From.Text = String.Empty
MsgBox("Invalid Range")

End If
If (CDbl(xtbColor1From.Text)) < (CDbl(XtbColor3To.Text)) AndAlso (CDbl(xtbColor1From.Text)) > (CDbl(XtbColor3From.Text)) Then
xtbColor1From.Text = String.Empty
MsgBox("Invalid Range")

End If
End If

Catch ex As Exception
End Try
End Sub
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

create component that inherit textbox,
now create property, maxVal & minVal

then overrite keypress event
and in key press event check, keychar is integer and it is < maxValue & > minVal

now build project, you will get this new control in toolbox
use it instead of normal texbox, set min & max value properties....

for runtime controlling max & min values, just take user input for both,
MyTxtbox1.MaxVal= convert.toint32(txtInputMax.text)
MyTxtbox1.MinVal= convert.toint32(txtInputMin.text)
MyTxtbox2.MaxVal= convert.toint32(txtInputMax.text)
MyTxtbox2.MinVal= convert.toint32(txtInputMin.text)

it is component so, you can use it in various projects and, it is also number kind of textbox then it will not need to validate every time, it will reduce your code.

you can also make properties like, decimal places... etc

some links for code
NumericTextBox (C#.NET)[^]
Numeric TextBox[^]
Numeric TextBox : Allow your users to enter numeric data the easy way[^][^]

use Numeic updown control / range validator
Textbox Max Min value[^]
Happy Coding!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

U can try like that :

in this example i have three textbox .in third textbox u cant enter the value between value of first two texbox.

Javascript function :

    function fnKeyPress(key) {
    var a = document.getElementById('txt1').value;
    var b = document.getElementById('txt2').value;
    if (key.key >= a && key.key <= b) {

        return false;

// .cs code 

 protected void Page_Load(object sender, EventArgs e)

        if (!IsPostBack)
            txt3.Attributes.Add("onkeypress", "return fnKeyPress(event)");

in same way u can try for more textbox.
Marcus Kramer 5-Oct-12 9:09am
solanki, please use full words in your answers. You are providing good solutions, but the textspeak really needs to go. 5-Oct-12 9:16am
Marcus Kramer 5-Oct-12 9:39am
:) (Although your response is a bit ironic I got a chuckle out of that) Cheers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100