Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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.
 
Example;
 
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 2:21am
Edited 5-Oct-12 2:22am
v2
Comments
Michael Siroen at 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 at 5-Oct-12 8:33am
   
I am working on WinForms, a desktop application...
DileepkumarReddy at 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
Try
'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
xtbColor1From.Focus()
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
ErrorLog(ex)
End Try
End Sub
Rate this: bad
good
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.
  Permalink  
Comments
Marcus Kramer at 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.
solanki.net at 5-Oct-12 9:16am
   
k
Marcus Kramer at 5-Oct-12 9:39am
   
:) (Although your response is a bit ironic I got a chuckle out of that) Cheers.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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,
then...
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[^]
http://stackoverflow.com/questions/9969824/vb-net-need-text-box-to-only-accept-numbers[^]
 
2.
use Numeic updown control / range validator
Textbox Max Min value[^]
Happy Coding!
Smile | :)
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 359
1 _Amy 230
2 Manfred R. Bihy 200
3 CHill60 180
4 OriginalGriff 175
0 OriginalGriff 7,495
1 Sergey Alexandrovich Kryukov 6,402
2 Maciej Los 3,849
3 Peter Leow 3,568
4 CHill60 2,702


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
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