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 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    strip out phone number characters

    I found this while searching for answers to my question.

    Originally posted by Vincent Puglia
    Hi,

    Before they move this to the db section...

    1) step through the field
    2) check each char
    3) if not integer, discard
    4) if integer, concatenate to var
    5) when finished with field, update with var.

    I'm assuming you really don't want to convert the phone number to an integer -- it's not (because you don't add, etc them) But, if you really want to:
    use intval()

    Vinny
    I want to do just that....evaluate each character and save only the numerical characters, but I don't know how to do this type of read (character in string).

    I'm new to JS. Please throw me a bone. I know about string length and loops.

    Thanks.
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    string.replace( /\D/g, '' ); => String with all non-digits stripped out. (Note that it will not change the original string, but return a new processed string.)


    Was this what you wanted?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help....but that is like a foreign language to me.

    What I woudl like to do....is set the value of a form field to a variable....loop thru the variable.....looking for common phone number characters like '(' and '-' or ')' . I would create a new string variable that contains only the numerical characters and put THAT string into the form textfield.

    Basically, I want the user to be able to put in their phone number in whatever format they want (555) 555-1221 or 555-555-1221 and have the actual form value be the string "5555551221". My data goes into a backend file and I can format later.

    I know how to do this in C++, but I'm VERY (like less than a week) new to JS and have the syntax knowledge of what I've learned over just one project.

    Thanks
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Then it seems you want to use the code I gave you. replace is a method of String instances such as the value of a form field. It takes a regular expression and a string as arguments, and will replace matches of the regular expression with the string in question. The regular expression I gave you (/\D/g) will match all non-digits, and they will be replaced by the string ''. The result will be sent as the return value from the function, a return value which you can for example write back to the originating form field, or even better write to a hidden form field, leaving the original form field as the user wrote it.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is an example:
    Code:
    <script type="text/javascript">
    function chkNum() {
    theNum = document.forms[0].Num.value;
    theNum = theNum.replace( /\D/g, '' );
    alert(theNum)
    }
    </script>
    </HEAD>
    
    <BODY>
    <form>
    Enter Phone Number: <input type="text" name="Num">
    <input type="button" value="Check It" onclick="chkNum()">
    </form>
    .....Willy

  • #6
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Willy and liorean.

    I've taken both your replies and now have just what I was looking for.

    I learn by example, so I appreciate the guiding syntax. I also took liorean's advice and put the value returned by the function to a hidden field. I could anticipate user's going back over and over to type in their phone number if they saw I had changed it.

    Thanks to both of you!
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #7
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How do I evaluate the length of a textfield on my form? I thought I knew this

    if(document.REG.wc_STRIPPEDPHONE.length != 10){
    alert("Please provide a valid phone number including area code.");
    document.REG.wc_PHONE.focus();
    }
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #8
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Had! to be something simple didn't it

    if(document.REG.wc_STRIPPEDPHONE.value.length != 10){
    alert("Please provide a valid phone number including area code.");
    document.REG.wc_PHONE.focus();
    return false;
    }
    -- Always thankful for kind and patient teachers willing to help a beginner.


  •  

    Posting Permissions

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