www.webdeveloper.com
Results 1 to 2 of 2

Thread: newUser: how do I prevent posting spaces in text field

  1. #1
    Join Date
    Aug 2005
    Posts
    1

    newUser: how do I prevent posting spaces in text field

    Hi there, I did a search but could find anything I could really grasp. I am VERY new to javascript, and just finished reading my first book on it- so please be patient if I seem ignorant (I am).

    I have been making a shoutbox as a personal challenge for a CMS, and have managed to make it work fine except users can post an 'empty' post by hitting the spacebar.

    Here is the code I am currently using in the head of the template:
    Code:
    <script type="text/javascript">
       function formCheck(el){
      if(el.value == ''){
         alert('Please Enter Something to Post!');
            el.focus();
               return false;
          } else {
                return true;
                }
          }
    </script>
    and this is in the body of the template:
    Code:
    <form method="post"  action="#xarModUrl('shouter', 'admin', 'create')#" onsubmit="return formCheck(document.getElementById('shout'));">
    		<p><input style="font-size:10px;" type="text" id="shout" name="shout" size="20" /></p>
    		<p><input type="hidden" name="oldurl" value="#$oldurl#" /></p>
    		<p><input  style="margin-top: 5px; font-size: 10px;" type="submit" name="submit" value="#$shoutbutton#" /></p>
    	</form>
    Now, I must stress that I pretty much understand this, but I had someone help me out a bit a while ago.

    Anyways, all works well if someone enters text-- it posts, and if someone just hits enter the alert window pops up. However, if someone hits the spacebar, an 'empty' post is submitted.

    I appreciate any help you may offer. Again, my apologies if this is a redundant post.


    Thanks

  2. #2
    Join Date
    Jan 2005
    Location
    Lithia Springs, GA USA
    Posts
    886
    A regular expression would work good there. One that checked to make sure that at the very least there was the characters a-z and A-Z as well as 1-0 this would weed out the empty posts.

    ::Edit:: It just occured to me that you probably won't know how to implement a regex. No problem here is a real basic one to help you get started...

    http://www.jibbering.com/faq/#FAQ4_16

    Remember that if a user has JavaScript turned off they will still be able to make a blank post. Therefore I suggest you also implement something to check the user input into your server side script!
    Last edited by BeachSide; 08-12-2005 at 09:03 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles