Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to make a javascript function that will prompt user to put space after 25 characters.Means it will be necessary for user to put space after 25 characters in a textbox which has total length of 58.

Waiting

Till now m using this
JavaScript
function nospaces(t){
  if(t.value.match(/\s/g)){
    alert('Sorry, you are not allowed to enter any spaces');
    t.value=t.value.replace(/\s/g,'');
  }
}

HTML
<input type="text" maxlength="58" name ="textbox" id="textbox" onkeyup="nospaces(this)">


but what it does ,it does not allow any space

ANy Help
Posted
Updated 17-Jul-12 2:27am
v3

look these articles on regular expression
Article1[^]
Article2[^]
Article3[^]
Article4[^]
 
Share this answer
 
v2
Try this:

the aspx markup for button

XML
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return checkSpace(this);" Width="485px" MaxLength="58"></asp:TextBox>


and the javaScript to achieve what you want

JavaScript
function checkSpace(sender)
   {
       var data = sender.value;

       if(data.length == 25 && data.substring(25,26) != ' ')
       {
           alert('please enter space now');
            return false;
       }
   }


I tested it on my end and it is working fine. Let me know if it works on your end too. if not I will try to refine my answer.
 
Share this answer
 
Comments
khan46 17-Jul-12 8:50am    
Its not working
Rahul Rajat Singh 17-Jul-12 8:56am    
What is the problem now. Tell me what is happening.
khan46 17-Jul-12 9:02am    
it is allowing character after 25. I need a prompt to say that you must put a space after 25 characters
StianSandberg 17-Jul-12 8:52am    
Isn't a little annoying getting a prompt while typing? Maybe a better solution is to automatically insert a space after 25 characters.. just a thought.
StianSandberg 17-Jul-12 8:54am    
changed my own solution as it was giving a prompt after each keyup ;)
JavaScript
function isValid(e){
   var value = e.value;

   // check if value is null
   if(value==null) {
     alert('value is null');
     return false;
   }

   // check if value is exact 58 characters long
   if(value.length!=58) {
     alert('value is not 58 characters long');
     return false;
   }

   // check if character 25 is a space
   if(value.substring(25,26)!=' ') {
     alert('character 26 is not a space');
     return false;
   }

   return true;
}


HTML
<input type="text" maxlength="58" name ="textbox" id="textbox" onblur="isValid(this)">
 
Share this answer
 
v9
Comments
khan46 17-Jul-12 8:53am    
it prompts 'value is not 58 characters long' whenever i press space
StianSandberg 17-Jul-12 8:54am    
just fixed it using event onBlur instead ;) That event is fired when you take focus away from the textbox. You may want to attach validation on submit.
<form onsubmit="return isValid()">

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



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