Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Help with some JavaScript

Help with some JavaScript

Scheduled Pinned Locked Moved Web Development
regexjavascripthelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Ingles
    wrote on last edited by
    #1

    I'm trying to modify a string so that any hyphens or spaces can be removed. At the moment, the code looks something like:

    var strNum = document.all[document.all["MyValidator"].controltovalidate].value;
    if(isNumberValid(strNum) && isCardTypeCorrect(strNum))
    return true;
    else
    return false;

    All I want to do is insert some code before the conditional so that any hyphens or spaces are removed from the strNum variable. I've tried using the .replace method with a /-/gi regular expression but that doesn't seem to work -- I've not done any regex's in JavaScript before so I've probably missed something pretty obvious! -- Paul "Put the key of despair into the lock of apathy. Turn the knob of mediocrity slowly and open the gates of despondency - welcome to a day in the average office." - David Brent, from "The Office" MS Messenger: paul@oobaloo.co.uk Download my PGP public key

    T 1 Reply Last reply
    0
    • P Paul Ingles

      I'm trying to modify a string so that any hyphens or spaces can be removed. At the moment, the code looks something like:

      var strNum = document.all[document.all["MyValidator"].controltovalidate].value;
      if(isNumberValid(strNum) && isCardTypeCorrect(strNum))
      return true;
      else
      return false;

      All I want to do is insert some code before the conditional so that any hyphens or spaces are removed from the strNum variable. I've tried using the .replace method with a /-/gi regular expression but that doesn't seem to work -- I've not done any regex's in JavaScript before so I've probably missed something pretty obvious! -- Paul "Put the key of despair into the lock of apathy. Turn the knob of mediocrity slowly and open the gates of despondency - welcome to a day in the average office." - David Brent, from "The Office" MS Messenger: paul@oobaloo.co.uk Download my PGP public key

      T Offline
      T Offline
      theJazzyBrain
      wrote on last edited by
      #2

      Ok... you go: strNum = parseString(strNum); function parseString(strNum){ while(strNum.indexOf(" ") != -1) { strNum= strNum.replace(" ",""); } while(strNum.indexOf("'") != -1) { strNum= strNum.replace("'",""); } return strNum; } Hope this helps:-D |---------------| | theJazzyBrain  | |---------------|

      P 1 Reply Last reply
      0
      • T theJazzyBrain

        Ok... you go: strNum = parseString(strNum); function parseString(strNum){ while(strNum.indexOf(" ") != -1) { strNum= strNum.replace(" ",""); } while(strNum.indexOf("'") != -1) { strNum= strNum.replace("'",""); } return strNum; } Hope this helps:-D |---------------| | theJazzyBrain  | |---------------|

        P Offline
        P Offline
        Paul Ingles
        wrote on last edited by
        #3

        Thanks for the info, as it happens, I found this nifty little regex:

        strNum = strNum.replace(/-|\s/g,"");

        -- Paul "Put the key of despair into the lock of apathy. Turn the knob of mediocrity slowly and open the gates of despondency - welcome to a day in the average office." - David Brent, from "The Office" MS Messenger: paul@oobaloo.co.uk Download my PGP public key

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups