Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 5-Oct-12 2:22am
v2
Comments
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
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

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...
VB
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!
:)
 
Share this answer
 
v3
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 :


C#
    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.
 
Share this answer
 
Comments
fjdiewornncalwe 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 5-Oct-12 9:16am    
k
fjdiewornncalwe 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900