473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Check blank text box using custom validator

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
9 5133
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike P | last post by:
I have a Custom validator that has ControlToValidate set to a textbox that also has a Required and RegularExpression validator. When I run the webpage on my machine the custom validator...
2
by: Reddy | last post by:
Hi, How to check whether a given number is an integer Thanks, Reddy
2
by: Abhishek Srivastava | last post by:
Hello All, Can I validate the minimum length of a field using any of the existing validators? I searched google on this subject and found people offering custom controls for this. However,...
9
by: Mike Moore | last post by:
We have 3 fields on our web form that makeup what could be a duplicate entry. We would like to look at these three fields that the user enters and check in the database to see the information in...
3
by: Andy | last post by:
Hi folks, I have a customvalidator control that works properly if it isn't contained in an ASP:TABLE. But, when I place it inside an ASP:TABLE, I find that _ServerValidate doesn't get fired,...
4
by: Rick | last post by:
Hello, I built a composite web control that has a textbox and a date control. added my custom control on a webform where there are other standard controls. Each control on the form has a...
4
by: Patrick Nolan | last post by:
I am using javascript to manipulate optgroups in select elements. When the results are displayed in Firefox 2.0 there is an annoying blank line at the top of the multi-line select box. This...
2
by: lstanikmas | last post by:
Hi, I'm validating a form with this ASP but receiving some blank email responses; does anyone see anything wrong with it?: function isFormVarExcluded(thisForm, strToCheck) { var strExcludeVars...
1
by: Lelu | last post by:
Hi, My HTML form is generating some blank email responses; does anyone see anything wrong with the scripts?: function isFormVarExcluded(thisForm, strToCheck) { var strExcludeVars =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.