Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CustomValidator does not fire when field is empty

62 views
Skip to first unread message

Johnny Random

unread,
Aug 2, 2002, 10:26:23 AM8/2/02
to
Here's a fun one for you:

I have a custom validator on a form, I want to display an error message if a
field is blank OR a dropdown list doesn't have something other than the
default selected. Guess what! If you tell the custom validator to check
the text field and there is nothing in the text field, the validator doesn't
fire! Yeaaaaa, I got hosed by Microsoft stupidity, yeaaaaaa! I can set the
validation control to the dropdown list, but I don't want that because it
does a postback to the server which ends up saving my record. Argh!

Basically, I want the user to type in the text field and be able to press
enter to post the form. If they don't type in the text field then they are
going to select one of the drop down reasons for why they are not typing in
the text field. I tried adding a RequiredField validator but of course that
give me the error when I pick one of the reasons and didn't have anything in
the text field.

So, does anyone know how to make a custom validator that will fire even if
there is nothing in the text field it's validating?


Marina

unread,
Aug 2, 2002, 10:32:37 AM8/2/02
to
Same thing happens with a regularexpression validator. I think MS wants us
to use a requiredfieldvalidator in conjunction with another custom one if
the custom one is necessary. Yes, it's stupid and annoying - but for some
reason they designed it so we need to use 2 validators instead of one...

"Johnny Random" <kic...@rabbit.com> wrote in message
news:OHIPxBjOCHA.444@tkmsftngp12...

SCole

unread,
Aug 2, 2002, 10:43:03 AM8/2/02
to
You could create a ClientValidationFunction for a custom validator against
the dropdownlist value. This will fire before it goes to the server side.

"Johnny Random" <kic...@rabbit.com> wrote in message
news:OHIPxBjOCHA.444@tkmsftngp12...

Johnny Random

unread,
Aug 2, 2002, 10:44:25 AM8/2/02
to
Yeah, but like I said, that doesn't work because the field is NOT always
required, it's only required if the dropdown list is not selected.

"Marina" <zlat...@nospam.hotmail.com> wrote in message
news:eVCHTFjOCHA.2512@tkmsftngp10...

Cowboy (Gregory A. Beamer)

unread,
Aug 2, 2002, 12:37:14 PM8/2/02
to
I am also a bit peeved by the way this was setup. I assume the idea was you
were either requiring the field (RequiredFieldValidator) or doing some other
form of validation. The solution I know works is to have both a
requiredFieldValidator and a CustomValidator on the same field. A bit of
overkill.

The other option is to subclass the CustomValidator and write your own
required event. This could be painful.

Hopefully, by ASP 2.0, they will add an attribute to make these controls
fire when fields are left empty, as well as when they have info in them.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
****
Think outside the box!
****************************************************************************
****


"Johnny Random" <kic...@rabbit.com> wrote in message
news:OHIPxBjOCHA.444@tkmsftngp12...

bruce barker

unread,
Aug 2, 2002, 1:52:10 PM8/2/02
to
it like ms never heard of cross fields checks. fortunately ms gives us the
source code, so it easy to fix. add you own attribute say
ValidateIfBlank="yes" to force validation if blank

<asp:CustomValidator id="myValidator"
ControlToValidate="Text1"
ClientValidationFunction="myValidate"
ErrorMessage="even if blank!"
ValidateIfBlank="yes"
runat="server"/>


then in WebUIValidation change

function CustomValidatorEvaluateIsValid(val) {
var value = "";
if (typeof(val.controltovalidate) == "string") {
value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
}
var args = { Value:value, IsValid:true };
if (typeof(val.clientvalidationfunction) == "string") {
eval(val.clientvalidationfunction + "(val, args) ;");
}
return args.IsValid;
}

to something like (air code not tested):

function CustomValidatorEvaluateIsValid(val) {
var value = "";
if (!val.ValidateIfBlank || val.ValidateIfBlank != 'yes')
{
if (typeof(val.controltovalidate) == "string") {
value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
}
}
var args = { Value:value, IsValid:true };
if (typeof(val.clientvalidationfunction) == "string") {
eval(val.clientvalidationfunction + "(val, args) ;");
}
return args.IsValid;
}


-- bruce


"Johnny Random" <kic...@rabbit.com> wrote in message
news:OHIPxBjOCHA.444@tkmsftngp12...

Joe

unread,
Aug 3, 2002, 9:32:16 AM8/3/02
to
Sounds like you might be able to code it using decision code such as "if
then else" to decide when and when not to use any particular validator.

--
Joe //replies to newsgroup only please//

"Johnny Random" <kic...@rabbit.com> wrote in message

news:e$9I2LjOCHA.1996@tkmsftngp12...

0 new messages