regular expression - 4 digits only OR null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stardotstar
    Member
    • Feb 2007
    • 83

    regular expression - 4 digits only OR null

    Hi guys,

    I had set a post code field as not mandatory, 4 characters, user editable etc, but when a used goes in and sets it to a null value it errors because the regular expression I used was:

    [0-9]

    what would I put in the reg exp field that would permit either a null entry OR 4 numerals?

    At the moment I have removed the reg exp altogether but that then causes other issues with a script that is expecting 4 digit postcodes as input.

    I can't see the way using the guide from prec help;

    Best regards,
    Will
  • Dody
    Senior Member
    • Aug 2004
    • 1896
    • 3.8.x

    #2
    it should be like:

    Code:
     
    ^[0-9]{0,4}
    while(true){
    if(
    $someone->needsHelp() && $i->canHelp()) $post->help();
    if(
    $i->findBug()) $post->bug();
    }

    Comment

    • stardotstar
      Member
      • Feb 2007
      • 83

      #3
      Thanks Dody, I will try this and post back when I properly understand the expression.

      Will


      ... So the anchor ^ facilitates a zero length entry and the 0-9 and 0,4 specify an otherwise not-null entry of up to 4 digits?
      Last edited by stardotstar; Tue 21 Jul '09, 5:30pm.

      Comment

      • Dody
        Senior Member
        • Aug 2004
        • 1896
        • 3.8.x

        #4
        Code:
        ^[0-9]
        means your input should only include digist from 0 to 9
        Code:
         
        {0,4}
        means your input should be from 0 to 4
        while(true){
        if(
        $someone->needsHelp() && $i->canHelp()) $post->help();
        if(
        $i->findBug()) $post->bug();
        }

        Comment

        • stardotstar
          Member
          • Feb 2007
          • 83

          #5
          Makes sense Dody - thank you.
          Why do we need the ^?

          Comment

          • Dody
            Senior Member
            • Aug 2004
            • 1896
            • 3.8.x

            #6
            ^ means the start of the string, as you only want digits, this will ensure that no other letters inserted before them.
            while(true){
            if(
            $someone->needsHelp() && $i->canHelp()) $post->help();
            if(
            $i->findBug()) $post->bug();
            }

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...