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
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
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>
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>
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.