[Javascript] Problem with Regular Expression

Problem with Regular Expression

Hi all,

First of all I must say that I am a newbie when it comes to javascript language.
Here is my problem.

I need validate in myForm this string:

09 - AB800

And try this Regular Expression:


if (!myForm.Folder.value.match("/^0[0-9] \\- [a-z0-9]{5,20}$/i", "")) {
     window.alert('Not valid.');
     myForm.Folder.focus();
     myForm.Folder.select();
     return false;
     }

But the correct string:

09 - AB800

is not validate. Why?

I need also optional add this other string:

09 - AB800/test

when the string part /test is not required.

Can you explain any one or any sample code related this…

Your help would be very appreciated.
thanks for your time and hints.

Thanks in advance,
Chevy.

Try the below, i turned off the capture groups expect for the entire match so it only returns the entire match and not everything.

Hello.
I’d appreciate your help so very much.

I’m sorry but the string 09 - AB800 is not validate in myForm


      if (!myForm.Folder.value.match("^(0(?:[0-9]+) \\- (?:[a-zA-Z0-9]+)(?:\\/[a-zA-Z]+)?)$", "")) {
          window.alert('Not valid');
          myForm.Folder.focus();
          myForm.Folder.select();
          return false;
          } 

Regular expressions in JavaScript require the forward and back slash but DON’T need the quotes around them, also the match function does NOT have a second argument as it returns the value to a variable assignment in which you would have declared. E.g.

var folder = myForm.Folder;

if (!folder.value.match(/^(0(?:[0-9]+) \\- (?:[a-zA-Z0-9]+)(?:\\/[a-zA-Z]+)?)$/)) {
    alert('Not valid');
    folder.focus();
    folder.select();
    return false;
}

Hello.
I’d appreciate your help so very much.

Now your suggestion working, but I have this problem:

  1. In my string I need max 2 numbers e.g. 09
  2. In my string I need min 5 and max 20 characters e.g. 09 - AB500 it’s ok, 09 - AB50012345678901 it’s wrong!

My first r.e.:

^0[0-9]{2} - [a-zA-Z0-9]{5,20}$/i

Can you help me?

See the updated expression below

folder.value.match(/([0-9]{2} \\- (?:[a-zA-Z0-9]{5,20})(?:\\/[a-zA-Z]+)?)/)

Hello.
I’d appreciate your help so very much.

I’m sorry, but this string is validate with the last reg exp:



folder.value.match(/([0-9]{2} \\- (?:[a-zA-Z0-9]{5,20})(?:\\/[a-zA-Z]+)?)/)

091 - AB50012345678901555555

See the below link, should be working fine now.

Thanks you very much for your help
I’m really happy for your quickly answer.
Good bye

Your welcome