kartal
kartal

Reputation: 18096

Don't allow spaces in TextBox asp.net

I am trying to avoid spaces in the Textbox and prevent the user to enter any spaces. I tried

<ajaxToolkit:FilteredTextBoxExtender ID="Filteredtextboxextender2" runat="server"
                                FilterType="Custom" InValidChars=" "
                                TargetControlID="tx_username">
                            </ajaxToolkit:FilteredTextBoxExtender>

but it didn't work. Is there any other solutions ?

Upvotes: 0

Views: 3762

Answers (2)

Mahmoud Fayez
Mahmoud Fayez

Reputation: 3459

You need client side validation please check all the options available for you:

http://msdn.microsoft.com/en-us/library/yb52a4x0(v=vs.100).aspx

also if I were you I would just use something like this:

$("#username").keypress(function(e) {
     if(e.which == 32) {
        e.preventDefault();
     }
})

which requires jQuery of course you can use pure Javascript if you like :)

Upvotes: 3

Andre Calil
Andre Calil

Reputation: 7692

According to the docs, you must set FilterMode="InvalidChars"

Upvotes: 2

Related Questions