Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    javascript replace

    document.write(str.replace(/(\+|\(|\)|.|-|\/ /g,''));

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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Try this:-

    Code:
    <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
    Last edited by Philip M; 01-24-2012 at 07:57 PM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    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 ]
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by felgall View Post
    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".

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder
    Join Date
    Jun 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    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

    Code:
    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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    You did not say that you wanted to validate a phone number.

    Any characters except 0-9 and + are invalid.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New Coder
    Join Date
    Jun 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    (123)123-1234
    +1(123)123.1234
    (123) 123 1234
    ...

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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 Phone Validation Javascript 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.
    Last edited by Philip M; 01-26-2012 at 05:05 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •