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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    About "value.replace" for Textareas

    I have a form named "contacter" and a textarea named "Comments". I need to find a way to prevent quoatations from being placed in the textarea and if the area placed in the textarea; I want them to be automatically deleted.

    I have tried to use:

    Code:
    onkeyup="this.value=this.value.replace('"','')"
    and also...

    Code:
    onkeyup="this.value=this.value.replace(''','')"
    But it will not work and I think the one single or double quotations marks cancels out the other one; when it is typed like this.

    I have been looking around and seen a few pieces of script, but have not been able to get them to work.

    I tried to create this; but it also did not work:

    Code:
    <script> 
    function check() 
    {
    if (window.event.keyCode=222) {
    document.contacter.Comments.value.length=-1;
    return false;
    } 
    else (void())
    } 
    </script>
    Any one know how I can get this textarea to automatically delete " and ' character, either onkeyup or onsubmit?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    If you are doing this to protect yourself on the server, you better do all of the replacing/escaping there since you can not rely on JavaScript or people like me using your forms.

    You probably want to use a regular expression

    .replace(/\'/gi,"");

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure quit how to do that. Can you give me an example on how to use Javascript .replace(/\'/gi,""); with my form fields.

    Form Name = contacter
    Form Field = Comments

    My main goal is to prevent "" and '. The rest of the chars, I am not really worried about.

    If you could give me an example of this, that would be very good. - Thanks

    I have got this so far; but it erases any value that is typed in the textarea and changes it to something else What am I doing wrong?

    <script>
    function check()
    {
    if (document.cf.Comments.value="'") {
    document.cf.Comments.value.replace(/\'/gi,"");
    return false;
    }
    else(void(0))
    }
    </script>
    </head>
    <body>

    <form name="cf">
    <textarea name="Comments" onkeyup="check()"></textarea>
    </form>
    Last edited by coder81; 01-10-2007 at 12:25 AM. Reason: updated

  • #4
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    try this...

    <SCRIPT type="text/javascript">
    function check(){
    var str=document.cf.Comments.value;
    str=document.cf.Comments.value=str.replace(/'/g, " ")
    document.cf.Comments.value=str.replace(/"/g, " ")
    }
    </SCRIPT>

    <form name="cf">
    <textarea name="Comments" onchange="check()"></textarea>
    </form>

  • #5
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That Works Great !!! - Thank You So Much !!!

  • #6
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Good, now help me to make this work for a variable, what how to do syntax???

    <SCRIPT type="text/javascript">
    function check(){
    var K=document.cf.myVar.value;
    var str=document.cf.Comments.value;
    str=document.cf.Comments.value=str.replace(/K/g, " ")
    document.cf.Comments.value=str.replace(/"/g, " ")
    }
    </SCRIPT>

    <form name="cf">
    <input id="myVar" value="M" onchange="check()">
    <textarea name="Comments" onchange="check()"></textarea>
    </form>

  • #7
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Wow, demanding

    ask nicely!

    Teach yourself about new RegExp() and see if you can figure it out.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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