VB.NET Textbox input validation

Hi Experts,

I have self designed a webpage .aspx (VB.NET).

It has the label (6 different fields) and textbox (1 field) controls.

I need to validate the text field as user inputs the value.

Conditions :
1. Net weight value should not exceed the balance quantity
2. Net weight value should not cross the order quantity limit (i.e. suppose ordered quantity is 100 and tolerance is 10% (+/-) then net weight field could accept the value between 90 and 110 otherwise alert should be popup.)
3. net weight should be in range between minimum and maximum weight. (respective of order quantity, balance quantity and tolerances)

Scenario :

Suppose,
Order Quantity is 2.500 Ton and tolerance (+/-) is 10%,
So the system should allow me to input net weight in range of 2.250 Ton and 2.750 Ton
if its value crosses that limit then system should prevent.

Now, out to 2.500 Ton some quantity (say 0.500 Ton) is picked. So the balance quantity will be 2.000 Ton. In this case also system will check the net weight value. If it crosses the limit system should prevent.

Accordingly, Minimum Weight and Maximum Weight is 0.500 - 0.600. This means per entry user can enter the value between 0.500 to 0.600 Ton. So, here also net weight field needs validation.

I have written the condition but it's not successful. Can anyone help me out so that I can apply successful validation on Net Weight field?

Please find my VB.NET code and scenario's for your kind reference.

If txtWeight.Text <> "" Then
                If (Val(txtWeight.Text) > Val(txtBalQty.Text)) Then
                    If Not ((Val(txtWeight.Text) >= (lblMinWeightBundle.Text - 0.01)) And (Val(txtWeight.Text) <= (lblMaxWeightBundle.Text + 0.03))) Then
                        MsgBox("Bundle Weight should be Minimum :" & (Val(lblMinWeightBundle.Text) - 0.01) & " Ton and Maximum : " & (Val(lblMaxWeightBundle.Text) + 0.03) & " Ton!!!")
                        txtWeight.Focus()
                        txtWeight.Text = ""
                    Else
                        If (Val(txtWeight.Text) >= (ordqty.text + (ordqty.text / 100) * lblQtyPluse.Text)) Then
                            MsgBox("Please Enter valid Weight as per the tolerance!!! Max :" & (ordqty.text + (ordqty.text / 100) * lblQtyPluse.Text) & " Ton" & vbCrLf & "Also Check Balance Quantity :" & txtBalQty.Text & "")
                            txtWeight.Focus()
                            txtWeight.Text = ""
                        End If
                    End If
                Else
                    If Not ((Val(txtWeight.Text) >= (lblMinWeightBundle.Text - 0.01)) And (Val(txtWeight.Text) <= (lblMaxWeightBundle.Text + 0.03))) Then
                        MsgBox("Bundle Weight should be Minimum :" & (Val(lblMinWeightBundle.Text) - 0.01) & " Ton and Maximum : " & (Val(lblMaxWeightBundle.Text) + 0.03) & " Ton!!!")
                        txtWeight.Focus()
                        txtWeight.Text = ""
                    Else
                        If (Val(txtWeight.Text) <= (ordqty.text - (ordqty.text / 100) * lblQtyMinus.Text)) Then
                            MsgBox("Please Enter valid Weight as per the tolerance!!! Min :" & (ordqty.text - (ordqty.text / 100) * lblQtyMinus.Text) & " Ton" & vbCrLf & "Also Check Balance Quantity :" & txtBalQty.Text & "")
                            txtWeight.Focus()
                            txtWeight.Text = ""
                        End If
                    End If
                End If
            End If

Open in new window


Scenario.PNG
Thanks in advance.

Regards,
D Patel
LVL 7
D PatelD Patel, Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shalu MCommented:
try using this piece of code. It's in C# but can easily be translated to VB.net

protected void Page_Load(object sender, EventArgs e)
        {
            var accept = ValidateWeight();
            Response.Write(accept);
        }

bool ValidateWeight()
        {

            if (string.IsNullOrEmpty(txtWeight.Text.Trim()))
                return false;


            var iNetWeight = double.Parse(txtWeight.Text.Trim());

            var iBalWeight = double.Parse(txtBalQty.Text.Trim());
            var iOrderQty = double.Parse(ordqty.Text.Trim());
            var iMinWeight = double.Parse(lblMinWeightBundle.Text.Trim());
            var iMaxWeight = double.Parse(lblMaxWeightBundle.Text.Trim());
            var iPlusTolerance = double.Parse(lblQtyPlus.Text.Trim()) / 100 * iOrderQty;
            var iNegTolerance = double.Parse(lblQtyMinus.Text.Trim()) / 100 * iOrderQty;

            Response.Write("iNetWeight: " + iNetWeight + " iBalWeight: " + iBalWeight + " iOrderQty: " + iOrderQty + " iMinWeight: " + iMinWeight + " iPlusTolerance: " + iPlusTolerance + " iNegTolerance :" + iNegTolerance);

            //1. input Weight should not exceed balance weight.
            if (iNetWeight > iBalWeight)
                return false;

            //2.
            if (iOrderQty > (iOrderQty + iPlusTolerance) || iOrderQty < (iOrderQty - iNegTolerance))
                return false;


            //3. checks for min and max weight
            if (iNetWeight < iMinWeight || iNetWeight > iMaxWeight)
                return false;

            // if all conditions are met return true
            return true;
        }
0
Chris JonesLead Application Web DeveloperCommented:
here is the converted code

Protected Sub Page_Load(sender As Object, e As EventArgs)
	Dim accept = ValidateWeight()
	Response.Write(accept)
End Sub

Private Function ValidateWeight() As Boolean

	If String.IsNullOrEmpty(txtWeight.Text.Trim()) Then
		Return False
	End If


	Dim iNetWeight = Double.Parse(txtWeight.Text.Trim())

	Dim iBalWeight = Double.Parse(txtBalQty.Text.Trim())
	Dim iOrderQty = Double.Parse(ordqty.Text.Trim())
	Dim iMinWeight = Double.Parse(lblMinWeightBundle.Text.Trim())
	Dim iMaxWeight = Double.Parse(lblMaxWeightBundle.Text.Trim())
	Dim iPlusTolerance = Double.Parse(lblQtyPlus.Text.Trim()) / 100 * iOrderQty
	Dim iNegTolerance = Double.Parse(lblQtyMinus.Text.Trim()) / 100 * iOrderQty

	Response.Write("iNetWeight: " + iNetWeight + " iBalWeight: " + iBalWeight + " iOrderQty: " + iOrderQty + " iMinWeight: " + iMinWeight + " iPlusTolerance: " + iPlusTolerance + " iNegTolerance :" + iNegTolerance)

	'1. input Weight should not exceed balance weight.
	If iNetWeight > iBalWeight Then
		Return False
	End If

	'2. 
	If iOrderQty > (iOrderQty + iPlusTolerance) OrElse iOrderQty < (iOrderQty - iNegTolerance) Then
		Return False
	End If


	'3. checks for min and max weight
	If iNetWeight < iMinWeight OrElse iNetWeight > iMaxWeight Then
		Return False
	End If

	' if all conditions are met return true
	Return True
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
D PatelD Patel, Software EngineerAuthor Commented:
Hi Experts,

my code is working fine, also there is no error.

Only issue is in my logic (which is posted in my question).

In addition, all conditions will be satisfied at a time then and only then net weight is accepted.

Scenario.PNGPlease suggest if any solution.

Regards,
D Patel
0
D PatelD Patel, Software EngineerAuthor Commented:
By the way, thanks for your warm support.

Your solution has resolved my issue.

Regards,
D Patel
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.