Click here to Skip to main content
15,885,208 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi huys i need to validate the textbox without allowing space starting position and allowing only one space between two words..

1234567
Ex:Vino Pr--->correct: 5th position is space i.e only one position is allowed.

123456789
Ex: Vino Pr--->This Example is incorrect 1st Position allowed Space and in between
allowed 2 spaces.. i need to textbox to get input not allowing the space in the 1st position and not allow the more than one space between the words.

i used the the Text.StartTrim().EndTrim()

it coming correctly while adding in the gridview from textbox.
But from Gridview to database it allowing the spaces.
Posted
Comments
Rahul Rajat Singh 2-Jul-12 22:58pm    
You need javascript to filter the keystroke based on these requirements or the validations.

Here I have created a JavaScript Method for validating the SPACE

no space allowed in first position and last position, there can be only one space in between any word

This function will Trim() the content and will check the space occurences if more than one space found then it will propmt "NOT VALID" else "VALID"

JavaScript
<script type="text/javascript">

    function AllowSingleSpaceNotInFirstAndLast() {
        var obj = document.getElementById('TextBox1');
        obj.value = obj.value.replace(/^\s+|\s+$/g, "");
        var CharArray = obj.value.split(" ");
        if (CharArray.length > 2) {
            alert("User name NOT VALID");
            return false;
        }
        else {
            alert("User name VALID");
        }
        return true;
    }
</script></script>



HTML
Username: <input type="text" id="TextBox1" name="user" />
<input type="button" value="Submit" onclick="return AllowSingleSpaceNotInFirstAndLast();" />



Mark it as solution if it answer your question
 
Share this answer
 
i think this code is helpfull to you
--------------------------------------------------------------
XML
<div>
   <script type=""text/javascript">

language="javascript">
       function isNumberKey(evt) {
           //    if (!(((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) || (event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 190) || (event.keyCode == 35) || (event.keyCode == 36)))
           //event.returnValue=false;
           var charCode = (evt.which) ? evt.which : event.keyCode
           if (charCode > 31 && charCode < 48 || charCode > 57)
               if (event.keyCode != 46)
                   if (event.keyCode != 110)

                       return false;

           return true;



       }


</script>
close the script tag
 
<asp:textbox id="txtAdm" runat="server" onkeypress="return isNumberKey(event)" xmlns:asp="#unknown">
                      Width="150px" MaxLength="8"></asp:textbox>

       </div>
 
Share this answer
 
Comments
Shemeer NS 3-Jul-12 7:53am    
he was asking for space validation, not number validation!!!
Member 10380324 5-Nov-13 2:01am    
How to do the same under button click using onclientclick of above javascript
Member 11933957 12-Sep-15 7:36am    
if i m entering space in textbox it accept value if i am using regular expression it not allow space between two words .i want to remove only leading space not. plzz help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900