PDA

View Full Version : String Manipulation


Fuego
03-30-2004, 01:56 PM
Trying to get a dot between every letter from a string (thats filled at a form at the Firstname field). So When I fill out AMA it should become A.M.A. well thats fixed withthe script right under here.
But when the user fills the form with the dots already in it like A.M.A.
the scripts makes A...M...A. of it. Can find whats wrong with the script

function Send(step) {
var step = step;
var aantal_pers = 0;
var intAantalPers = parseInt(document.formulier.aantalpax.value) + parseInt(document.formulier.aantalbby.value);
var strBezetting = "";
document.formulier.antverkmr.value = <%=intKmr%>;
if (controleer_formulier()) {
for (var i = 1; i <= intAantalPers; i++) {
var VoorlLength = 0;
var strVoorlres = '';
VoorlLength = eval("document.formulier.dlnvorltri" + i + ".value.length");
for (j = 1; j <= VoorlLength; j++) {
if (eval("document.formulier.dlnvorltr" + i + ".value.substring(" + (j-1) + "," + j + ")") != ".") {
strVoorlres = strVoorlres + (eval("document.formulier.dlnvorltri" + i + ".value.substring(" + (j-1) + "," + j + ")") + '.');
}
}

Danne
03-30-2004, 03:44 PM
I think you can solve it easier using regular expressions. Here's an article (http://www.evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/index.html) about those.


String.prototype.addDots=function(){
return this.replace(/\./gi, "").replace(/([a-z])/gi, "$1.");
}
alert("t.es.t.".addDots()); // returns t.e.s.t.


The first replace removes all dots, the second replaces all letters with the letter itself and a dot.