2

I have the following code in Titanium to check if user input is non-numeric:

textBox.addEventListener('change', function (e) {
    var patt = new RegExp('^\d+(\.\d{1,2})?$','gi');
    if (e.value.match(patt) != null) {
        //insert action here
    }
});

I personally would like to delete non-decimal characters when a user tries to input one. However, in order to do so, I would need to use replace(inversePatt, ""). I would like to know, how do I get the inverse of my regular expression?

Pink Jazz
  • 784
  • 4
  • 13
  • 34
  • You mean the complement? Inverse of a reg-ex doesn't make much sense.. – Josiah Hester Oct 29 '13 at 16:07
  • I guess so. I would like to delete all non-numeric characters from my user input, as well as any extra decimal points after the first. – Pink Jazz Oct 29 '13 at 16:08
  • Take a look at this SO question about getting the complement of any regex.[How do I turn any regex into an complement of itself without complex hand editing?](http://stackoverflow.com/q/3977455/874257) – Josiah Hester Oct 29 '13 at 16:09
  • Could you just do `if(e.value.match(patt) == null)` then replace / remove unwanted characters? – Josiah Hester Oct 29 '13 at 16:11
  • Okay, I tried using your statement and did `textbox.value = textBox.value.slice(0, -1)`, however, now my app locks up on numeric input. Does anyone know why? – Pink Jazz Oct 29 '13 at 16:30

2 Answers2

3

to delete non-decimal characters, you should be able to match every:

[^\.\d]

group returned.

([^.\d] should work - here a dot needn't be escaped)

The carat inverts inside brackets. It basically means "not a dot or a number".

Check out:

http://www.scriptular.com

EDIT: I think this has your answer:

Restricting input to textbox: allowing only numbers and decimal point

EDIT 2: You could also use this:

var success = /^\d*\.?\d{0,2}$/.test(input);

as per:

Limiting input field to one decimal point and two decimal places

you can also demand a number before the decimal point like so:

var success = /^\d+\.?\d{0,2}$/.test(input); // uses + instead of *
Community
  • 1
  • 1
Plasmarob
  • 1,321
  • 12
  • 20
  • I tested this on the string: "adsf3.344564564 56trrty ert" Try to give examples of text you want to match. – Plasmarob Oct 29 '13 at 16:29
  • However, I want to restrict the input to only one decimal point as well. Your example lets me enter more than one decimal point. – Pink Jazz Oct 29 '13 at 16:33
  • you may not need to have an entire regex for the whole box. just check each new char as it comes in. See the link. – Plasmarob Oct 29 '13 at 16:47
  • If I do that, how would I know if there is a second decimal point? I would need to know if there is more than one decimal point, and I don't see how I would do that by checking each new character. – Pink Jazz Oct 29 '13 at 17:09
  • then obviously you're going to need to use a regex on it to see if there already is one. see my edit. maybe that will help. – Plasmarob Oct 29 '13 at 17:21
  • Thanks. Your solution worked and my app is no longer locking up. – Pink Jazz Oct 29 '13 at 17:43
  • 1
    Side note, you *don't* need to escape a dot in a character class. So `[^.\d]` is absolutely fine – HamZa Oct 29 '13 at 19:10
  • i suspected so - but didn't want to cause confusion. thanks for making that clear. – Plasmarob Oct 29 '13 at 20:11
1

If you want to handle chars in a key event, something like below should work.
As an acceptance validation, use something like \d to make sure a digit is present.

 # ^(?:\.|\d*|\d+\.|\d*\.\d+)$
 # "^(?:\\.|\\d*|\\d+\\.|\\d*\\.\\d+)$"


 ^                       # BOL 
 (?:
      \.                 # dot, or 
   |  
      \d*                # optional digits (if no digits, matches ^$) 
   |  
      \d+ \.             # digits and dot
   |  
      \d* \. \d+         # optional digits and dot and digits
 )
 $                       # EOL