SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    [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:
    Code:
    09 - AB800
    And try this Regular Expression:
    Code:
    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:
    Code:
    09 - AB800
    is not validate. Why?

    I need also optional add this other string:
    Code:
    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.
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try the below, i turned off the capture groups expect for the entire match so it only returns the entire match and not everything.

    http://regexr.com?312hg

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello.
    I'd appreciate your help so very much.

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

    Code:
          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;
              }
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    Code JavaScript:
    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;
    }

  5. #5
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.:
    Code:
    ^0[0-9]{2} - [a-zA-Z0-9]{5,20}$/i
    Can you help me?
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    See the updated expression below

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

  7. #7
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello.
    I'd appreciate your help so very much.

    I'm sorry, but this string is validate with the last reg exp:
    Code:
    folder.value.match(/([0-9]{2} \- (?:[a-zA-Z0-9]{5,20})(?:\/[a-zA-Z]+)?)/)
    
    091 - AB50012345678901555555
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  8. #8
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    See the below link, should be working fine now.

    http://regexr.com?312i8

  9. #9
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  10. #10
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your welcome


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •