Home > ASP.Net, Javascript > Allow only one Space in TextBox

Allow only one Space in TextBox


Try this example using Javascript

you can add this function to your textbox’s onblur event or any you want to check..

 

   <script type="text/javascript" >
        function checkSpace()
        {
            var txt = document.getElementById('TextBox1');
            var strText = txt.value;
            var strArr = new Array();
            strArr = strText.split(" ");
            for(var i = 0; i < strArr.length ; i++)
            {
                if(strArr[i] == "")
                {
                    alert("there must be one space char between words!");
                    break;
                }
            }
        }
    </script>

<asp:TextBox ID="TextBox1" runat="server" onblur="checkSpace();"></asp:TextBox>

//—————-Or——————

function AllowSpace(txt)
{
   var CharArray=txt.value.split('');
   var count=0;
   for(var i=0;i<CharArray.length;i++)
   {
     if (CharArray[i] == " ")
     {
        if(count == 0)
        {
            count = count + 1;
        }
        else
        {
          return false;
        }
      }
   }
   return true;
}

<asp:TextBox ID="TextBox1" runat="server" onblur="AllowSpace(this);"></asp:TextBox>

 

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: