0

I am trying to constrain a textbox to either digits OR one of two special string cases: "na" or "tx". If digits exits in the input, "na" and "tx" are invalid and should not be allowed in the input box. if "na" or "tx" exists, digits should not be allowed.

I am very new to reg expressions but is it possible to construct this? Here is what I have so far:

event.sender.element.find("input").keyup(function () {
    this.value = this.value.replace(/((?!na)|([^0-9]))/g, '');
});

Using basic online examples, I know how to limit the input to purely digits. My current set of requirements, however, is making this very challenging for me.

TAS
  • 383
  • 1
  • 6
  • 17

2 Answers2

2
string = string.replace(/^(n(a?|$)|t(x?|$)|[0-9]*).*$/g,'$1');

Though, generally, using onkeypress & onchange events in tandom yield better results: the first prevents the 'flickering' of the invalid characters, the second prevents changing it by pasting in data with the mouse for instance. See also: How to prevent number input on keydown As always: revalidate it on the server.

Community
  • 1
  • 1
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • that works awesome! but when you type something like "txc" or "nas" it resets back to "t" or "n", how can it be made to revert to "tx" or "na" instead in that case? – FiLeVeR10 Oct 24 '13 at 00:10
  • 1
    Ah, small change indeed: `.replace(/^(n(a?|$)|t(x?|$)|[0-9]*).*$/g,'$1');`, thanks for pointing it out. – Wrikken Oct 24 '13 at 00:20
  • is there a way to make the string "t5" switch to numbers with this regex, to fit the parameters of the issue. since at that point it does match a number, but doesn't match "tx" or "na"? – FiLeVeR10 Oct 25 '13 at 00:56
  • What do you mean? It leaves any digits as is atm? – Wrikken Oct 25 '13 at 08:36
  • Ah. "t5" => invalid (I read "15" in a hurry this morning, hence my confusion), whether we want to keep the "t" from "tx", or the "5" of "any number" is up for debate, the default now is the first character typed determines the rest, which IMHO is a good default. You can switch it around if you like. – Wrikken Oct 25 '13 at 12:50
  • This is a great solution! Thanks a lot Wrikken! And to others for chiming in and posting alternative solutions! I am marking this as the accepted answer. – TAS Oct 29 '13 at 17:46
-1
$('input').keyup(function () {
    var a = $(this).val();
    if (a.match(/^tx/)) {
        a = 'tx';//set manually
    } else if (a.match(/^na/)) {
        a = 'na';//set manually
    } else if(a.match(/[\d]/)) {
        a = a.replace(/[^0-9]+/g, '');//replace all but numbers
    } else if (a.match(/^[nt]/)) {
       a = a.replace(/^([tn])([a-zA-Z])+$/,'$1');//replace with first letter
    } else {
        a = '';//matches nothing equals nothing
    }
    $(this).val(a);//print new value
})

It uses regex to remove any undesired input, and provides reporting on the demo. If it doesn't start with 'tx' or 'na', but does have a number it switches to number mode, and if it does begin with either of them then it's hard set to them. If nothing matches, it empties out.

EDIT

Although my answer produced the correct result, it wasn't as simple as it should be with regex.

So I added Wrikkens regex as well, since it's easier: http://jsfiddle.net/filever10/FQ5aD/

The biggest difference i see in functionality is with strings like "t5", on wrikkens, that gets changed to "t", and on mine that triggers number mode and will change to "5".

This allows it to fit the parameters of the problem. Since neither 'tx' or 'na' exist at that point, numbers should take over because 't' and 'n' don't match anything yet, but a number does; since it only needs one character to match as a number, and the others need 2 for a match.

FiLeVeR10
  • 2,155
  • 1
  • 12
  • 13
  • How is this not useful? – FiLeVeR10 Oct 25 '13 at 10:31
  • 1
    Not sure who downvoted without giving reason, but I think your solution is still valid, since it does handle the description of my problem. Thanks for posting! – TAS Oct 29 '13 at 17:48