By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,509 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,509 IT Pros & Developers. It's quick & easy.

Check blank text box using custom validator

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi,

show bit more the code you have. Are you sure the txtBox refers correctly to
the TextBox?

If you need more background info about ASp.NET validation (also custom
validators) see this article:
http://msdn.microsoft.com/library/de...pplusvalid.asp

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Alex Shirley" <po******@alexshirley.com> wrote in message
news:OU**************@TK2MSFTNGP09.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #2

P: n/a
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...

"Alex Shirley" <po******@alexshirley.com> wrote in message
news:OU**************@TK2MSFTNGP09.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

---
Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004
Nov 18 '05 #3

P: n/a
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?
Alex Shirley <po******@alexshirley.com> wrote in message news:<OU**************@TK2MSFTNGP09.phx.gbl>...
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).

Nov 18 '05 #4

P: n/a
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #5

P: n/a
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
---------------------------------------------------------

Cheers!

Alex
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #6

P: n/a
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...

"Alex Shirley" <po******@alexshirley.com> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
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
---------------------------------------------------------

Cheers!

Alex
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

---
Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004
Nov 18 '05 #7

P: n/a
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/de...pplusvalid.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
"Mike Smith" <te**@test.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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...

"Alex Shirley" <po******@alexshirley.com> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
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
---------------------------------------------------------

Cheers!

Alex
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

---
Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004

Nov 18 '05 #8

P: n/a
hey teemu !
thats a great tip ! thanks...

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
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/de...pplusvalid.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
"Mike Smith" <te**@test.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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...

"Alex Shirley" <po******@alexshirley.com> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
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
---------------------------------------------------------

Cheers!

Alex
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

---
Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004


---
Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004
Nov 18 '05 #9

P: n/a
Thanks for this ...!

Alex

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.