Results 1 to 3 of 3

Thread: Filtered Textbox (REGEX)

  1. #1
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Filtered Textbox (REGEX)

    Hi,

    I'm trying to only allow limited characters to be entered into a textbox using a regex I have the correct expression but cannot seem to get it to work?

    HTML Code:
    <script language="javascript" type="text/javascript">
    
    
    function myNumberFilter(txb) {
    
      var filter = new RegExp("-{0,1}[0-9 \,]*");
      if (!filter.test(txb.value))   txb.length -= 1;
    
    
    }
    
    </script>
    
    <form>
    <input type="text" id="test" onKeyUp="myNumberFilter(this);"  />
     </form>

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try:
    Code:
    var filter = new RegExp("[-]?[0-9 \,]*");
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. The Following User Says Thank You to Master_script_maker For This Useful Post:

    dke01 (04-07-2008)

  4. #3
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks I got it working with the following code

    HTML Code:
     var re = new RegExp("-{0,1}[0-9]*$","gi"); 
            var m = re.exec(txb.value);
            
          if (m != txb.value) 
             txb.value = txb.value.substring(0, txb.value.length-1);
          
          }

Bookmarks

Posting Permissions

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