2

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

1
  • 1
    if you got the answer then please select your answer... Commented Jan 9, 2014 at 5:42

3 Answers 3

2

Try

function validateTextBox()
{
    var txt = document.getElementById('<%= TextBox1.ClientID %>').value;
        if(txt.charAt(0)==' '){
           alert("No space allowed in the beginning");
           }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.