Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Regular Expression Help

Options
  • 01-02-2011 12:29pm
    #1
    Registered Users Posts: 81 ✭✭


    Hi

    I have a requirement to meet the following criteria:

    * Textbox that can accept only numeric characters
    * Must be 6 digits in length
    * Cannot be a full-length repeating number (e.g. 444444 is bad, 444144 is ok)
    * Cannot increment or decrement sequentially (e.g. 012345 and 987654 are bad)


    I've spent some time trying to get my head around the regular expression 'mini language' but it's just not playing ball for me. Was wondering if any RegEx wizards here might be able to help please?



    Thanks in advance for any assistance


Comments

  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    I would recommend doing the following:
    Go here and download Expresso. It's a free RegEx tool and I found it a great help when I was trying to get my head around the syntax. Expresso comes with an analyser which can help you pinpoint flaws in logic.

    One other thing you might do is, for example, restrict user input in the textbox to numeric characters only, that way your RegEx validation won't have to take that into account.


  • Registered Users Posts: 1,109 ✭✭✭Skrynesaver


    if ((/^\d{6,}^/) && ( ! /^(.)\1+$/) && (! is_incremental() )){
    //We're cool
    }
    
    is_incremental/decremental is probably better implemented as a boolean function than as a regex. In fact it would be a hiddeous regex with much backtracing that would grind slowly, whereas as a boolean function you could step forward by one char and get the character at that index as an integer and compare it to the previous character to see if it differed by your increment which would be set to 1 or -1 by the first comparison.


Advertisement