0

Hi I'm working on a form validation script. Up to this point, I've only written very basic regex validations. The input that I'm trying to validate should have entries in the following format AAA111 separated by comma space. So some sample entries are : ABC111 or ABC111, ABC112, ABC113. Here's what I have so far:

if(!entry_54.match(/^[A-Z]{3}[0-9]{3}/)){
            //do something
        }

But this only catches a match at the beginning of the string. Any tips on how to expand the regex?

4
Contributors
8
Replies
10
Views
5 Years
Discussion Span
Last Post by grant.baker
-1

it's really easy!

function vali_pattern(text){
    if(text.length==6)
        if(text.charAt(0)<='Z' && text.charAt(0)>='A')
            if(text.charAt(1)<='Z' && text.charAt(1)>='A')
                if(text.charAt(2)<='Z' && text.charAt(2)>='A')
                    if(text.charAt(3)<='9' && text.charAt(3)>='0')
                        if(text.charAt(4)<='9' && text.charAt(4)>='0')
                            if(text.charAt(5)<='9' && text.charAt(5)>='0')
                                return  true;
    return false;
}
0

That will only check for a single entry of, say, AAA111 right?. AAA111 is a valid entry, but so is AAA111, AAA112 and ABC123, AAA111, AAA113. I need to check that each item entered as AAA111 is separated by a comma and a space. And it has to end with an AAA111 entry. But I don't know how many entries in the AAA111 format there will be.

2

this work will done easily via JavaScript RegExp :

    if(!entry_54.match(/^(?:[A-Z]{3}[0-9]{3}[,][ ])*[A][A][A][1][1][1]$/)){
        //do something
    }

Tell me if you have any problem with it.

Edited by hr.Ziggurat

2

Spot on, only too many (unnecessary) brackets ;) /^(?:[A-Z]{3}[0-9]{3}, )*AAA111$/ should do the same.

0

Guys, thank you so much for the help! I had to modify it a bit to /^(?:[A-Z]{3}[0-9]{3}, )*[A-Z]{3}[0-9]{3}$/, as I want to accept any input that is [A-Z]{3}[0-9]{3}, not only AAA111. Just to be clear that I understand how this regex is working, ^(?:[A-Z]{3}[0-9]{3}, )* says that there should be a match of, say, ABC123 0 or more times at the very beginning of the string. And [A-Z]{3}[0-9]{3} says that next there should be a match of, say, XYZ789. $ means that there should be nothing after. Am I correct in my understanding of the syntax?

0

$ means that there should be nothing after

Basically yes. $ means end of the string, ^ start of the string. It's use changes slightly when you use the multiline modifier.

1

You can keep the regexp simple, as follows :

if(((entry_54.match(/[A-Z]{3}[0-9]{3}/g) || ['']).join(', ')) !== entry_54) {
    //do something
}

This works on the basis that if the matching parts joined back together with the separator yield the original string, then there's a match, otherwise no match.

[''] is included to cater for there being no matches, in which case .match() will return null and .join() would throw an error.

To make the test intolerant of entry_54 == '', simply replace [''] with ['dummyString']. The dummy string needs to be unlikely ever to be entered by a user and can be made as strong as you wish (like a password).

I guess this approach will be less efficient than a more comprehensive regexp on its own, due to the .join() and string comparison. The advantage would be primarily in shorter regexp development time rather than execution efficiency.

Another plus is that this approach is highly amenable to be generalized in the form of a utility function, eg :

function testRepeatingPattern(str, pattern, separator, tolerateEmptyString, matchFn, failFn) {
    var dummy = tolerateEmptyString ? '' : '$d^u^m^m^y^S^t^r^i^n^g%';//good and strong
    var fn = (((str.match(pattern) || [dummy]).join(separator)) === str) ? (matchFn || function(){return true;}) : (failFn || function(){return false;});
    return fn();
}

Play around with it here.

Edited by Airshow: Improved text

0

Airshow, thank you for adding your input! I had forgotten that I actually do need to allow no input, and your script allows for that. It works perfectly for what I need to do! And thank you also for the detailed description for this JS noob!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.