Check blank text box using custom validator

A

Alex Shirley

Hi there

I’m simply trying to check for a blank or empty value in a textbox on my
webform. In this instance I do not want to use a requiredfieldvalidator,
I want to use a customvalidator (as I have other code within the
customvalidator which works).

This won’t work (within servervalidate):

If txtBox.Text.ToString = "" Then raise exception…..

Nor will this

If txtBox.Text = "" Then raise exception…

I’ve tried null (perhaps I didn’t do that correctly) but is doesn’t seem
to work either, any ideas?

Seems a really simple thing to do (but /sigh! I just can't do it).

Thanks

Alex
 
M

Mike Smith

u raising an exception ?
with the customvalidator , as long as u link the controltovalidate property
u just need to code the login of when the condition is valid.. the
serverValidate event has an args parameter,, u need to set it to either true
or false/

If txtBox.Text.ToString="" then
args.IsValid=false
else
args.IsValid=true
end if

u can add the same in a client side script also and link the
clientvalidation function property...
 
D

DujHoD

For a CustomValidator, you don't want to raise an exception; you want
to modify the ServerValidateEventArgs that's passed in, like this:

Sub ValidateTextBox(sender as Object, args as
ServerValidateEventArgs)
Dim valid As Boolean
valid = (txtBox.Text.Length > 0)
' Do other stuff here...
args.IsValid = valid
End Sub

Instead of txtBox.Text, you could also use args.Value, which would
arguably make your code more maintainable:

valid = (args.Value.Length > 0)

Clent-side validation works in much the same way. Is this what you're
asking about?
 
A

Alex Shirley

Thank for this

My initial impressions from your posts are that I'm doing everything
right (which is probably flawed!).

I think I'd better look at this tomorrow under a fresh perspective and
will provide some feedback.

Thanks

Alex
 
A

Alex Shirley

Weird, any ideas with this ....?

-----------------------------------------------------------
This code works
-----------------------------------------------------------

'If user types hello, the custom validator flags up
Private Sub cvProductStartDate_ServerValidate(ByVal source As
System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
cvProductStartDate.ServerValidate

If txtProductStartDate.Text.ToString <> "hello" Then
args.IsValid = True
Else
cvProductStartDate.ErrorMessage = "You typed hello"
args.IsValid = False
End If
End Sub

-----------------------------------------------------------
This code doesn't work
-----------------------------------------------------------

'Checks for a blank textbox, does not work!
Private Sub cvProductStartDate_ServerValidate(ByVal source As
System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
cvProductStartDate.ServerValidate

If txtProductStartDate.Text.ToString <> "" Then
args.IsValid = True
Else
cvProductStartDate.ErrorMessage = "Type something in"
args.IsValid = False
End If
End Sub
 
M

Mike Smith

well i guess thats the idea why they made the requiredfield validator ?
cause thats the only control that checks that.. and u suppose to use the
various validation controls in conjuction with each other.. so place a
required validator to check for blanks and then ur custom one with what ever
logic u need to check...

i guess thats why in the books they say the required field validator is THE
ONLY validator that can check for blanks ? well thats the line ive read
someplace...
 
T

Teemu Keiski

Hi,

just to add that if you use CustomValidator but don't specify the
ControlToValidate property it will fire the validation despite if control is
empty or not (because the control is not specified, it runs the validation
always and therefore you could validate empty controls as well with it).

See this article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspplusvalid.asp

The behaviour is described there:
"You can leave the ControlToValidate blank. In this mode, the server
function always fires once per round trip and the client function always
fires once for each attempt to submit. You can use this to validate controls
that cannot otherwise be validated, such as a CheckBoxList or stand-alone
radio buttons. It can also be useful when the condition is based on multiple
controls and you don't want it evaluated as the user tabs between fields on
the page"

But the result is that you can also use it for blank controls...
 
M

Mike Smith

hey teemu !
thats a great tip ! thanks...

Teemu Keiski said:
Hi,

just to add that if you use CustomValidator but don't specify the
ControlToValidate property it will fire the validation despite if control is
empty or not (because the control is not specified, it runs the validation
always and therefore you could validate empty controls as well with it).

See this article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspplusvalid.asp

The behaviour is described there:
"You can leave the ControlToValidate blank. In this mode, the server
function always fires once per round trip and the client function always
fires once for each attempt to submit. You can use this to validate controls
that cannot otherwise be validated, such as a CheckBoxList or stand-alone
radio buttons. It can also be useful when the condition is based on multiple
controls and you don't want it evaluated as the user tabs between fields on
the page"

But the result is that you can also use it for blank controls...

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
 

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,774
Messages
2,569,596
Members
45,131
Latest member
IsiahLiebe
Top