Web Analytics Made Easy -
StatCounter RegExp problem, wont match space - CodingForum

Announcement

Collapse
No announcement yet.

RegExp problem, wont match space

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RegExp problem, wont match space

    Hi

    Im trying to match up a postal code with a space in it
    "[A-Z][0-9][A-Z]\s[0-9][A-Z][0-9]"

    It works fine without the space (\s)

    Can anyone help, Ive tried so many different ways but still doesnt work

    function checkPostal(form){
    var CanadianZipPattern="^[A-Z][0-9][A-Z]\s[0-9][A-Z][0-9]$";
    var postalRegExp = new RegExp(CanadianZipPattern);
    if (postalRegExp.test(form.value) == true )
    alert("valid");
    else
    alert("not");
    }

  • #2
    Re: RegExp problem, wont match space

    Does this: "[A-Z][0-9][A-Z][\s][0-9][A-Z][0-9]"?
    www.united-scripts.com
    www.codebattles.org

    Comment


    • #3
      It's because you enter the \s in a string. The backslash will be evaluated in the string, thus you will get '^[A-Z][0-9][A-Z]s[0-9][A-Z][0-9]$'.

      Instead, try
      Code:
      function checkPostal(form){
          var
               postalRegExp = /^[A-Z][0-9][A-Z]\s[0-9][A-Z][0-9]$/; [color=green]// should this be case sensitive? If not, add an [color=blue]i[/color] flag after the end slash.[/color]
          if(postalRegExp.test(form.value))
              alert("valid");
          else
              alert("not");
      }
      liorean <[[email protected]]>
      Articles: RegEx evolt wsabstract , Named Arguments
      Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
      Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

      Comment


      • #4
        thanx so much!! (ended hours of frustration) lol

        it works now liorean

        thanx again
        ViperHell

        Comment

        Working...
        X