[thelist] numeric only regex? [ and one more perhaps?]

Tom Dell'Aringa pixelmech at yahoo.com
Wed Aug 3 07:48:06 CDT 2005


> Are you saying that you want 1 to infinity digits to return true 
> exclusively? That any character other than a digit should return false? 
> Then you could do:
> 
> ^[0-9]*$

That is indeed what I needed, so I ended up with exactly that:

var nummatch = /^[0-9]*$/.test(numtext.value);

> The thing about regex that makes it hard to grok sometimes is that regex is 
> a patter MATCHING language, not a procedural language.  It searches for 
> matches exclusively, so you have to think "only what is there". It's a 
> great mind twister. I love it.

Yeah, I *still* don't have my head around it, which I why I struggle with it everytime it comes
up. I think it's neat, just never have time to play with it.

> By the way, what's the context of this? Sometimes it helps if we know what 
> you are wanting to do. Sometimes regex is only one way to go.

We are validating some inputs on the client side to make sure the action doesn't get bad data. One
is a customer number - thus I wanted only numbers, and this works great.

The other one is the customer NAME. The requirements for this one are:

- Minimum 3 characters
- Alpha only
- We must allow a wildcard character of "*"

So valid would be: abc, abc*, abcde etc
and invalid input: ab*, ab, 1a*, 1a

For that we are using:
---
if (nametext.value.length > 0) {
        var noWildCards = nametext.value.split("*");
        if (noWildCards[0].length < 3) {
            alert("The customer search criteria must have at least 3 characters (and may also have
optional wildcards).\nYou entered: "+nametext.value);
            return false;
        }
    }
--

It seems to work, but I'm wondering if there is a better regex way to do it instead...

Thanks

Tom
    return true;


http://www.pixelmech.com/

Melissa: Ace, Where are you?
Ace Ventura: I'm in Psychoville and Finkle's the Mayor.



More information about the thelist mailing list