Reputation: 205
How can I disable space as first character in the textbox in asp.net c# using jquery?
Upvotes: 2
Views: 752
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
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
Upvotes: 0
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