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
    Nov 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation HELP! Filtering validating single quote character out!

    Hello CF nuts....
    Im working on a submission form which I want to validate with JavaScript...
    I need help to get the sinqle quote out of the input fields....
    So when someone inputs a forbidden character like a quote (') the javascript will respond with an error message....Can someone help me with this...
    Since a lot of people are using quotes in there texts and its messing up my database....
    Thanks in advange...

    Roger

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Well I'm not exactly sure whether you want to allow everything BUT ' or whether you only want alphanumeric characters.

    Well for alphanumeric, use the following:
    Code:
    if (/[^a-z0-9]/i.test(fieldname.value)){
    	alert("invalid");
    }
    For searching for just the ' character use:
    Code:
    if (fieldname.value.indexOf("'") != -1){
            alert("Invalid character ' entered!");
    }
    Hope this is of some use
    Last edited by Badman3k; 12-07-2004 at 10:43 PM. Reason: Some of my code was wrong, now I've rectified it.
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #3
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Well I tried to put the code in a from and It doesn't work for me...
    Maybe Im doing something wrong???Please help....

    <script LANGUAGE="JavaScript">
    function FcheckForm(f){
    <!--
    if(username.value.indexOf("'") != -1){ alert("Invalid character ' entered!");return false; }
    }
    return true;
    }
    }
    -->
    </script>
    <form method="POST" action="testfrm.html" id="frmform" name="frmform" onSubmit="return FcheckForm(this);">
    <input name="username" type="text">
    <input type="Submit" name="btnSubmit" value="Submit">
    </form>

  • #4
    New Coder
    Join Date
    Dec 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script LANGUAGE="JavaScript">
    function FcheckForm(f){
      if(f.username.value.indexOf("'") != -1){ 
        alert("Invalid character ' entered!");
        return false; 
      }
      return true;
    }
    </script>

  • #5
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Ooh my god forget to put the function value into the check.....
    Working now....thanks!!!!

  • #6
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Ideally though I think you want to just escape the single quote, if you are just disallowing it do to the database errors. No need to limit the user.

    Somthing like this:

    Code:
    <script type="text/javascript">
    var x=escape("I think Harry's head is going to explode!!  That's bad news.");
    alert(x);
    x=unescape(x);
    alert(x);
    </script>
    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You should escape the single quotes in the server-side and not in the client-side. If you're using ASP, you escape it by using another one. See this sticky for more info.
    Glenn
    ____________________________________

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


  •  

    Posting Permissions

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