...

View Full Version : javascript replace



wcipolli
01-24-2012, 08:09 PM
document.write(str.replace(/(\+|\(|\)|.|-|\/ /g,''));

feeble attempt to remove '.' '(' ')' ' ' '-' '/'and '+'

Philip M
01-24-2012, 08:43 PM
Try this:-


<script type = "text/javascript">
var str = "Something + '.' '(' ')' '-' '/'";
var x = str.replace(/[+\.'()\-/]/g,"");
alert (x);
</script>

I understand that you wish to remove all those characters specified in the character class, that is within the square brackets. In a character class it is necessary to escape the hyphen (make be interpreted it as a literal) as otherwise a hyphen is used to indicate a range of characters, such as a-z.

Remember that any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

A man generally has two reasons for doing things - the one that sounds good, and the real one. - J.P.Morgan

felgall
01-24-2012, 09:11 PM
In a character class it is necessary to escape the hyphen (make be interpreted it as a literal) as otherwise a hyphen is used to indicate a range of characters, such as a-z.

You can avoid the need to escape the hyphen in that situation by placing it last immediately before the ]

Philip M
01-25-2012, 08:39 AM
You can avoid the need to escape the hyphen in that situation by placing it last immediately before the ]

True! :) But why not just escape it to be safe! And the dot (.) does not have to be escaped in a character class, but I always do so just for clarity as . in a pattern means "any character".

felgall
01-25-2012, 08:48 AM
True! :) But why not just escape it to be safe! And the dot (.) does not have to be escaped in a character class, but I always do so just for clarity as . in a pattern means "any character".

There's nothing wrong with escaping things to be safe. I was just pointing out that there is a spot you can put it so as to not need to escape it. If you remember to put it last AND to escape it then if you forget one of them then it will still work. If you only know about one and manage to forget it then it will be broken.

wcipolli
01-26-2012, 03:45 PM
var regexInternational = /^\+(?:[0-9] ?){6,14}[0-9]$/;// ITU-T E.123 format +international phone number with only space separator
var regexEPP = /^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/;// +CCC.NNNNNNNNNNxEEEE

^^ Will check for industry standards


function validateInternationalPhoneNumber(number){
number= number.replace(/\+|\.|\(|\)|\-| /g,'');
if(IsNumeric(number)&&(number.length<15&&number.length>7){
return true;
}
else{
//need to add error message for invalid
}
}

^^will just check for a number between 7 and 15 with no illegal characters.
Allows numbers, +, ., (,),-, and spaces.

Philip M
01-26-2012, 05:05 PM
You did not say that you wanted to validate a phone number.

Any characters except 0-9 and + are invalid.

wcipolli
01-26-2012, 05:08 PM
(123)123-1234
+1(123)123.1234
(123) 123 1234
...

Philip M
01-26-2012, 05:49 PM
Just strip the unwanted characters and count the resulting length - 7 to 15 numbers. Ensure that any + is the first character, and only one + is present.

Have a look at http://www.codingforums.com/showthread.php?t=156981 and many other threads in this forum.

If you allow brackets etc. then the user can type silly things like ()(+ and still pass the validation.

But I thought you were trying to strip brackets, hyphens and so on. Now you seem to want to allow them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum