Help with some JavaScript
-
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 -
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 keyOk... 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 | |---------------| -
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 | |---------------|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