user2124167
user2124167

Reputation: 205

Textbox validation in jquery disabling space

How can I disable space as first character in the textbox in asp.net c# using jquery?

Upvotes: 2

Views: 752

Answers (3)

MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

Try

function validateTextBox()
{
    var txt = document.getElementById('<%= TextBox1.ClientID %>').value;
        if(txt.charAt(0)==' '){
           alert("No space allowed in the beginning");
           }
}

Upvotes: 2

Amit
Amit

Reputation: 15387

You can achieve this using JQUERY as below:

$(function(){
    $("#a").keypress(function(evt){
         var cc = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
        if($(this).val().length==0){
            if(cc==32)
                 return false;
             }
    });

});

a is the your textbox Id

DEMO

Upvotes: 0

kiro
kiro

Reputation: 111

This problem isn't related to C#/asp.net. All u need is javascript.

just try to catch the onkeydown event.

$("#text").keydown(function(){
    var txt = $(this).value;
    if(txt.charAt(0)==' '){
       alert("No space in the beginning");
       txt.value(txt.replace(" ",""));//clear first space in text
     }
});

text is id of input

Upvotes: -1

Related Questions