Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Match non-numbers in input field

Last post 08-26-2008, 3:43 AM by prometheuzz. 1 replies.
Sort Posts: Previous Next
  •  08-26-2008, 3:18 AM 45639

    Match non-numbers in input field

    I have a text input field that I want only to accept numbers or the strings "min" and "max"

    I am using the javascript replace function and have the code (/[^0-9]/gi,"") that will get rid of all characters that aren't numbers, what do I need to add so that it will still keep the strings min and max?  I thought something like ^min would work but it doesn't seem to do anything.

    Regards.

  •  08-26-2008, 3:43 AM 45640 in reply to 45639

    Re: Match non-numbers in input field

    What happens in the following cases:

    123min456

    1min

    max2

    minmax

    1m2a3x4

     ?

     

    How about doing it in two steps: 1) check to see if the string matches "min" or "max", 2) if it doesn't, replace all non-digits:

    var text = "123ef456d";
    if(!text.match(/^(?:min|max)$/gi)) {
      text = text.replace(/[^\d]/gi, '');
    }

View as RSS news feed in XML