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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replacing Apostropes in a textfield.

    I have a form textfield where I want to substitute all apostrophes with a blank.

    So if someone enters State's in my textfield called Comments it will replace the entry with State s.

    Here is my attempt for implementing this on the Comments textfield only:

    Code:
    <script>
    function CommentsReplace()
    {
           document.myformName.Comments.replace(/'/,' ');
    }
    </script>
    
    
    ....
    <form name="myFormName" action="myactionName.cfm" onsubmit="return CommentsReplace();">
    ...
    Please advise why this is not working and how I can get it to work??

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    First of all, you need to change the .value property of the field, not the field itself.

    Second, String.replace is nondestructive, meaning it leaves the orignial as is but returns the string wit5h the replacements done. So, you need to do something like
    Code:
    Field.value=Field.value.replace(re);
    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
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script type="text/javascript">
    <!--
    
    function reCom(){
    var doc = document['myFormName']['Comments'];
    var replace = doc.value.replace(/'/,' ');
    doc.value = replace;
    alert(replace);
    }
    
    //-->
    </script>
    
    
    ....
    <form name="myFormName">
    <input type="text" name="Comments" />
    <input type="button"  onclick="javascript:reCom();" />
    </form>
    ...


    You also need to watch your casing:

    document.myformName.Comments.replace(/'/,' ');
    }
    </script>
    ....
    <form name="myFormName" action="myactionName


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Or simply pass the form reference to the function.
    Code:
    function CommentsReplace(oForm)
    {
        oForm.Comments.value=oForm.Comments.value.replace(/'/,' ');
    }
    ...
    <form onsubmit="return CommentsReplace(this);">
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the additional information.


  •  

    Posting Permissions

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