简体   繁体   中英

Regex is allowing characters not specified in the pattern?

I need to ensure a field on my form contains only alphanumeric characters. Zero through Nine, A through Z. No punctuation, no special characters, nothing else.

I have the following method:

function foo()
{
    var pStrValue = mTrim($('#txtIDNumber').val());

    var regexFirstChar = new RegExp("^[A-Z0-9]{1}"); //First character is alphanumeric
    var regexNum = new RegExp("^[0-9]{9}.{0,3}$"); // First 9 are numeric
    var regexLetter1 = new RegExp("^[A-Z]{1,3}[0-9]{6}$"); //Up to the first 3 are alpha, then there are exactly 6 numbers
    var regexLetter2 = new RegExp("^[A-Z]{1,3}[0-9]{9}$"); //Up to the first 3 are alpha, then there are exactly 9 numbers

    var firstCharIsNum = !isNaN(pStrValue.charAt(0));

    if (!regexFirstChar.test(pStrValue)) //If the first character isn't alphanumeric
        return false;
    else if (firstCharIsNum) 
    {
        //this is the conditional that evaluates to true incorrectly
        if (!regexNum.test(pStrValue)) //If the first character is a number and is not proceeded by 8 more digits
            return false;
    }
    else if (!firstCharIsNum) 
    {
        if (!regexLetter1.test(pStrValue) && !regexLetter2.test(pStrValue)) //If the first 1-3 characters are letters and are not proceed by exactly 6 or 9 digits
            return false;
    }

    return true;
}

The problem is that this is accepting special characters. I entered 1234567890”,'” into the textbox and this passes validation.

I wrote this a year ago and it was definitely working then (or I suppose perhaps QA missed this), but since then our application has gone under a significant rewrite. In either case, regex definitely isn't my strong suit. Why is this allowing special characters?

If I understand your goal correctly, the problem is in your line:

var regexNum = new RegExp("^[0-9]{9}.{0,3}$");

The . allows any character between zero and three times. At a minimum, you need to escape it as \\. -- but I think what you're really looking for is (for nine digits before the decimal point, and three digits after):

var regexNum = new RegExp("^[0-9]{9}\.[0-9]{0,3}$");

Your regexNum allows (after 9 numbers) up to 3 chars, no matter what:

var regexNum = new RegExp("^[0-9]{9}.{0,3}$");

So you could simply remove this part and it will only allow exactly 9 digits

var regexNum = new RegExp("^[0-9]{9}$");

Here you may test it: http://regexr.com/3eaa5

edit: With 3 optional alphanumeric values (upper- or lowercase) after the 9 numbers, it would be:

var regexNum = new RegExp("^[0-9]{9}[A-Z0-9]{0,3}$");

If i intend your question regexNum tests if the string is made of exactly 9 digits + 0-3 alphanumeric characters [0-9A-Z]. If so:

var regexNum = new RegExp("^[0-9]{9}[0-9A-Z]{0,3}$"); // First 9 are numeric + 0-3 of any alphanumeric characters, end of string.

As you requested this allows no punctuation, no special characters, nothing else, just 0-9 and AZ.

You can use the following RegEx

var regularExp = new RegExp("^[0-9]{0,9}.{0,3}$");

Explaination:

^ assert position at start of the string
[0-9]{0,9} match a single character between 0 to 9
$ assert position at end of the string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM