validate multiple controls

W

Willem

I have got a very simple javascript which compares about 8 controls in
pairs of two. i.e. maxA - minA <= 25.
Now I would like it to be triggered for validation (before submit?) and
cancel the submit if neccessary. Very easy to do if it were plain HTML.
No idea how to implement this in ASP.NET 2 though. Can someone please
point me in the right direction?

the script:
function checkDifferences()
{
var msg;
if (txt_MinA.value != "" && txt_MaxA.value != "")
{
if (txt_MaxA.value - txt_MinA.value > 25)
{
msg = "A: The difference can't be over 25 mm.\n";
}
}
if (txt_MinB.value != "" && txt_MaxB.value != "")
{
if (txt_MaxB.value - txt_MinB.value > 25)
{
msg += "B: The difference can't be over 25 mm.\n";
}
}
if (txt_MinC.value != "" && txt_MaxC.value != "")
{
if (txt_MaxC.value - txt_MinC.value > 25)
{
msg += "C: The difference can't be over 25 mm.\n";
}
}
if (txt_MinD.value != "" && txt_MaxD.value != "")
{
if (txt_MaxD.value - txt_MinD.value > 25)
{
msg += "D: The difference can't be over 25 mm.\n";
}
}
return msg;
}
 
D

dkode

On the onclientclick of the submit button, call a javascript function
that will validate the controls, if something doesnt check out, then
you can use javascript to cancel the post.

Sorry i dont have any code, not at my home computer right now.
 
W

Willem

thanks for responding. It means I have to add the code using the
stringbuilder, so I get the proper clientid's of the controls. Bit of
work, but no problem.

Do you happen to know how I can cancel the submit from this event?
Never mind: just solved that with:
OnClientClick="if(!checkDifferences())return false;".
Weird that OnClientClick="checkDifferences();" doesn't work.

Thanks for the tip.

dkode schreef:
 
V

ValliM

Hi Willem

The below code do all the validation required by you in server side. The
validation is done on the Click Event of the SubmitBtn. If the validation
results holds true the Submit button will be enabled otherwise it will be
disabled.

protected void SubmitBtn_OnClick(object sender, EventArgs e)

{

bool validOutput = CheckForValidation();

if (validOutput == true)

{

SubmitBtn.Enabled = true;

}

else

{

SubmitBtn.Enabled = false;

}

}

protected bool CheckForValidation()

{

if ((txt_MinA.Text != string.Empty) && (txt_MaxA.Text != string.Empty))

{

int AMaxValue = System.Convert.ToInt16(txt_MaxA.Text);

int AMinValue = System.Convert.ToInt16(txt_MinA.Text);

if (AMaxValue - AMinValue > 25)

{

return false;

}

if ((txt_MinB.Text != string.Empty) && (txt_MaxB.Text != string.Empty))

{

int BMaxValue = System.Convert.ToInt16(txt_MaxB.Text);

int BMinValue = System.Convert.ToInt16(txt_MinB.Text);

if (BMaxValue - BMinValue > 25)

{

return false;

}

}

else

{

return false;

}

if ((txt_MinC.Text != string.Empty) && (txt_MaxC.Text != string.Empty))

{

int CMaxValue = System.Convert.ToInt16(txt_MaxC.Text);

int CMinValue = System.Convert.ToInt16(txt_MinC.Text);

if (CMaxValue - CMinValue > 25)

{

return false;

}

}

else

{

return false;

}

if ((txt_MinD.Text != string.Empty) && (txt_MaxD.Text != string.Empty))

{

int DMaxValue = System.Convert.ToInt16(txt_MaxD.Text);

int DMinValue = System.Convert.ToInt16(txt_MinD.Text);

if (DMaxValue - DMinValue > 25)

{

return false;

}

}

else

{

return false;

}

}

else

{

return false;

}

return true;


}

Let me know if u have any doubts.

Regards,

Valli

[www.syncfusion.com
http://www.syncfusion.com/faq/aspnet/default.aspx]
 
W

Willem

lol ... very nice Vallim. I appreciate the effort you put into it. But
this would leave me with a submitted form (with unvalidated data) and
no opportunity to repost it with correct data as the submit button has
been disabled.

Of course I can show a message instead of disableing the button, but I
rather do my validation client-side.
 
D

dkode

Just my 2cents,

I always try to use validation controls from dotnet, I try to shy away
from doing client side validation.

It might be easier to implement and use less bandwidth, but in the long
run if business requirements change in your middle tier, then you are
forced to change the middle tier and the outputted javascript code that
is sent to the client.

Just makes for more places you are spreading your business requirements
to, and I always see client side javascript as a maintenance headache.

Just a matter of preference depending on the project, I have done
clientside validation before when absolutely required, but I try to
stay away from it.

sean
 
W

Willem

The dotnet controls use client script as well. They are simply not
sufficient though. Especially if you need to validate between multiple
controls. The base validator object expects a single control and not a
collection or controls. I actually assume this will be different in
asp.net 3.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top