1,105,406 Community Members

How, using a CustomValidator, to make sure a textbox isnt empty?

Member Avatar
riahc3
 
1,404 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
0
 

Hello

I want to use a CustomValidator using ASP.NET/C# to make sure a textbox isnt empty (no spaces, no blanks, no nulls, etc)

How do I do it?

Thanks

Member Avatar
shine_jose
Light Poster
48 posts since Jul 2009
Reputation Points: -6 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

use required field validator and set the control to validate in required field validator properties to Your text box name.set validation group of button control and validator control same Give some error message in Text property of validator. if you want use custume validator itself

first go to aspx page (source view) and past

<asp:CustomValidator ID="validator_name" runat="server" 
                            ControlToValidate="textbox" 
                            ErrorMessage="errortext." onservervalidate="custumvalidatorfunctionname"></asp:CustomValidator>
                        </td>

go to code

  protected void test(object source, ServerValidateEventArgs args)
    {
// write your test code here
if(textbox.text.trim().Length > 0)
custumvalidatorname.isvalid =true;
else
custumvalidatorname.isvalid = flase;
    }

ButtonClick event()
{
if(args.isvalid)//it will execute only when custum validator is valid
{
write the code you need
}
}

or tool manu
add one custom valiadator from tool box its self.
Hope this helped you sorry less time more work

Member Avatar
Freon22
Light Poster
48 posts since Feb 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

While shine's method is good and will work fine. He is using the server side code to validate the textbox.

There is another way, you can validate on the client side instead. Below you will see I am using the requiredfieldvalidator. On my apps I alway place a red star before or after any required fields. Its just to let the user know that he/she has to provide the information. Anyway when the server sends the page it also writes the javascript for you to validate that the user did type something into the textbox

<asp:TextBox ID="FirstName" runat="server" MaxLength="20"></asp:TextBox>
<asp:Label ID="FirstNameStar" runat="server" ForeColor="Red" Text="*"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldFirstName" runat="server" Text="First Name Required!"</asp:RequiredFieldValidator>
Member Avatar
Freon22
Light Poster
48 posts since Feb 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

So sorry, I re-read your question and see you want NO whitespaces. I hate doing Expressions, everytime I use them I have to re-read how to write them. Someday I will get it straight. LOL

Ok so here it is fro clientside validating. The first RequiredField Validator checks to see that the textbox is not empty. The RegularExpression Validator checks to see that where are no white spaces between point 0 and point 20. If you look at my textbox you will see that it has a maxlength of 20 so I can only type in a max of 20 letters. I can type in Freon22 and it will validate ok but if I type in Freon 22 it will fail. I can type in a max of 20 letters and it will pass as long as there are no whitespaces between any of the letters or numbers.

Hope this helps???

<asp:TextBox ID="FirstName" runat="server" MaxLength="20"></asp:TextBox>
<asp:Label ID="FirstNameStar" runat="server" ForeColor="Red" Text="*"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldFirstName" runat="server" Text="First Name Required!" ControlToValidate="FirstName"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Text="No White Spaces" ValidationExpression="\S{0,20}" ControlToValidate="FirstName"></asp:RegularExpressionValidator>
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article